i need a custom boundfield inside a grid where i have two controls inside.
i thought of something like this:
Public Class MyBoundColumn Inherits BoundField Private name As String Private userId As String Public Sub New() Dim hfUserId As New HiddenField hfUserId.Value = userId Dim lblName As New Label lblName.Text = name cell.Controls.Add(hfUserId) cell.Controls.Add(lblName) End Sub end class
but as you for sure know this is not working like this.
i need inside the cell a label or literal where the username is shown, and a hidden field where the userid is the value. the username and userid are both coming from the sql datasource of the grid.
I know there is a way to do this in design view with templates and eval(). But for different reasons i really need it as a class
i hope this works and you can help.
The above code is just for a demostration purpose only, there are several ways you can get the UserName and UserId from sql datasource. What are you trying to do actually??
Please "Mark as Answer" if this post answered your question.
lebsites
Member
2 Points
12 Posts
custom databoundfield with two controls
Dec 13, 2012 04:51 AM|LINK
hi,
i need a custom boundfield inside a grid where i have two controls inside.
i thought of something like this:
but as you for sure know this is not working like this.
i need inside the cell a label or literal where the username is shown, and a hidden field where the userid is the value. the username and userid are both coming from the sql datasource of the grid.
I know there is a way to do this in design view with templates and eval(). But for different reasons i really need it as a class
i hope this works and you can help.
thanks :-)
2pac
Participant
1586 Points
269 Posts
Re: custom databoundfield with two controls
Dec 13, 2012 05:08 AM|LINK
can you please re-post the question in correct format.
Thank you
Regards,
Jayesh
lebsites
Member
2 Points
12 Posts
Re: custom databoundfield with two controls
Dec 13, 2012 05:20 AM|LINK
sorry,
done
2pac
Participant
1586 Points
269 Posts
Re: custom databoundfield with two controls
Dec 13, 2012 05:42 AM|LINK
Hi,
you need to use RowDataBound event
example: create a template field in your gridview and add a asp:PlaceHolder shown below
<asp:TemplateField HeaderText="User Name"> <ItemTemplate> <asp:PlaceHolder ID="PlaceHolder" runat="server"></asp:PlaceHolder> </ItemTemplate> </asp:TemplateField>in code behind
private string name; private string userId; protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { //find the place holder in each row var placeHolder = e.Row.FindControl("hiddenPlaceHolder"); HiddenField hfUserId = new HiddenField(); hfUserId.Value = userId; Label lblName = new Label(); lblName.Text = name; placeHolder.Controls.Add(hfUserId); placeHolder.Controls.Add(lblName); } }Hope this helps
Regards,
Jayesh
lebsites
Member
2 Points
12 Posts
Re: custom databoundfield with two controls
Dec 13, 2012 12:01 PM|LINK
hi,
thanks for your reply!
but where do the values from username and userid come from? since they are both in the sql datasource i dont know how to access them.
thank you :-)
2pac
Participant
1586 Points
269 Posts
Re: custom databoundfield with two controls
Dec 13, 2012 11:58 PM|LINK
The above code is just for a demostration purpose only, there are several ways you can get the UserName and UserId from sql datasource. What are you trying to do actually??
Regards,
Jayesh