How to Find Control in Templatefield of Gridview

How to find Control in TemplateField of GridView?

Try this:

foreach(GridViewRow row in GridView1.Rows) {
if(row.RowType == DataControlRowType.DataRow) {
HyperLink myHyperLink = row.FindControl("myHyperLinkID") as HyperLink;
}
}

If you are handling RowDataBound event, it's like this:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink myHyperLink = e.Row.FindControl("myHyperLinkID") as HyperLink;
}
}

How to find a control inside a TemplateField in Gridview

You can't do like this. When you click the button: B_Load, then GridView is NOT in Edit mode. And this is why you can't get the TextBox, which is in EditItemTemplate.

You can only get the controls inside <ItemTemplate> in your button click as gridview is in Normal display Mode. <EditItemTemplate> controls are rendered only when GridView enters Edit mode.

So, you need to get the value of the Label: Label1 here actually, which has the same value and is inside <ItemTemplate> .

 Dim Lbl_Control As Label= CType(row.FindControl("Label1"), Label)

// button click as usual, just get and check the value of Label control, rather than TextBox control.

Protected Sub B_Load_Click(ByVal sender As Object, ByVal e As EventArgs) '(sender As
Object, e As System.EventArgs) Handles B_Load.Click
Dim FullText As String = ""
For Each row As GridViewRow In GV_Comments.Rows
Dim CB_Control As CheckBox = CType(row.FindControl("Comment_Select"),
CheckBox)
Dim Lbl_Control As Label= CType(row.FindControl("Label1"), Label)
If CB_Control IsNot Nothing AndAlso CB_Control.Checked AndAlso Lbl_Control
IsNot Nothing Then
FullText = FullText & Lbl_Control.Text & "<br/>"
End If
Next row
CompiledText.Text = FullText.ToString
End Sub

find Control in TemplateField of GridView with jquery or javascript

Thanks for including the GridView example. Now that I can see what you are attempting, I have a much better answer for you.

First, make a slight change to the button template, change out CommandArgument for OnClientClickand since you are using this button client side instead of posting back to the server, you can simplify it like this:

<asp:Button ID="btn" Text='btn' OnClientClick='<%# Eval("ID", "YourJavascriptFunction({0} - 1); return false;") %>' runat="server" CausesValidation="false" />

I have the click event call your JavaScript function and it sends in a parameter of the server side resolved id. Notice I subtract 1 first though. This is because the server side ASP.Net Eval function give the ID starting at 1. But, each of the ids that get generated for your text input elements start with a zero base.

Now look at the JavaScript function below.

// Clicking the first button sends in a 0, second sends in a 1, etc.
function YourJavascriptFunction(id) {
// each of the TextBox1 elements has an ASP.Net server side
// generated id that ends with GridView2_Textbox1_0,
// GridView2_Textbox1_1, etc.
let selectId = "input[id$='GridView2_Textbox1_" + id + "']";

// The text we use in the querySelector function states
// find the DOM element with a tag of "input" where the id
// ends with . . . The $ in id$= is the part that says the
// value must "end with"
let textBox1 = document.querySelector(selectId);

// Now that we have TextBox1 from the same row as the button,
// getting the value is easy.
alert(textBox1.value);
}

I left off a jQuery example as this querySelector command works in almost every browser including IE8 and above, so you shouldn't need jQuery for something this simple.

Let me know if I can help further.

Gridview row.Cells[i].Findcontrol finding control in wrong Cell

ok - turns out that calling row.cells(0).FindControl("") is the equivalent of calling row.FindControl("") because FindControl() operates within the current NamingContainer. And all the cells in a given row have the same NamingContainer, specifically {System.Web.UI.WebControls.GridViewRow}

If all you need is to determine the column index of a given control try this (pardon the VB):

Public Function getCellIndexByName(ByVal row As GridViewRow, ByVal ColumnName As String) As Integer
For ci As Integer = 0 To row.Cells.Count - 1
If CType(row.Cells(ci), DataControlFieldCell).ContainingField.ToString() = ColumnName Then
Return ci
End If
Next

Return -1
End Function

how to find control in gridview

You need to handle the RowCommand event on the gridview since the button click event bubbles up to the gridview control.

<asp:GridView id="GridView1" AutoGenerateColumns="false"
OnRowCommand="OnGridRowCommand" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn"
CommandArgument="<%# Eval("ID") %>"
Text="Click" CommandName="foo" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Code behind:

protected void OnGridRowCommand(object sender, GridViewEventArgs e)
{
//e.CommandSource - reference to the button
//e.CommandName - the command name, in this case foo
//e.CommandArgument - use it to find which row the button click originated from


}

Now if you are trying to find your other controls do something like:

System.Web.UI.HtmlControls.HtmlGenericControl O1Radio =
(System.Web.UI.HtmlControls.HtmlGenericControl)
((Button)e.CommandSource).Parent.FindControl("O1");

Proper use of FindControl() in GridViews

After some experimentation, I've found that the following works perfectly fine.

Label lblSomething = (Label)e.Row.FindControl("lblSomething");
Label lblSomethingElse = (Label)e.Row.FindControl("lblSomethingElse");

Whether this is "best practice" is beyond me.

Also, I've removed runat="server" from the first TemplateField in the markup to match the others. It's unnecessary, apparently. (Who knew?)

<Columns>
<asp:TemplateField HeaderText="Online materials available to assign">
<ItemTemplate>
<asp:Label ID="lblThis" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Assign" HeaderStyle-Width="75px">
<ItemTemplate>
<asp:Label ID="lblThat" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Assign" HeaderStyle-Width="75px">
<ItemTemplate>
<asp:Label ID="lblSomething" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Assign" HeaderStyle-Width="75px">
<ItemTemplate>
<asp:Label ID="lblSomethingElse" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>

</Columns>


Related Topics



Leave a reply



Submit