Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Dec 13, 2012 11:58 PM by 2pac
Dec 13, 2012 04:51 AM|LINK
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.
Dec 13, 2012 05:08 AM|LINK
can you please re-post the question in correct format.
Dec 13, 2012 05:20 AM|LINK
Dec 13, 2012 05:42 AM|LINK
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">
<asp:PlaceHolder ID="PlaceHolder" runat="server"></asp:PlaceHolder>
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;
Hope this helps
Dec 13, 2012 12:01 PM|LINK
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 :-)
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??