문제

I am trying to databind fields in a formview from the codebehind. If I were to do it in the front end, it would be something like:

<asp:TextBox id="txtField" runat="server" text='<%# Bind("col_name") %>' />

and col_name would be present in the select and update commands of the sqlDataSource bound to the parent FormView that the textbox is in.

As the form is generated dynamically in the codebehind, I can't use the <%# Bind() %> syntax. How can I bind the textbox from the codebehind? If I can't, what is the best solution to this?

도움이 되었습니까?

해결책

You can just use Databinder.Eval in Code behind.

Me.txtRun.Text = DataBinder.Eval(ObjectOnPage, "PROPERTYNAME")

If you are doing this in the dgData_RowDataBound Gridview Method you can use e.Row.DataItem

Protected Sub dgData_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles dgData.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        DirectCast(e.Row.FindControl("txtControl"), TextBox).Text = DataBinder.Eval(e.Row.DataItem, "Property")
    End If
End Sub

More info can be found in the following link:

DataBinder.Eval Method (Object, String)

I hope this helps.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top