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