|
Hi,
I'm using Visual Studio and ASP to generate a web page. On it is a Gridview with several columns. I want to have access from JavaScript to change data client-side in one of those (non-key) columns. So I used:
<asp:TemplateField HeaderText="Percentages">
<ItemTemplate>
<asp:Label ID="PERCSIT" runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="PERCS" runat="server" Text='Even geduld a.u.b.'></asp:TextBox>
</EditItemTemplate>
<ControlStyle BackColor="#00CC00" Width="35px" />
</asp:TemplateField>
However, when this is rendered, no ID is generated since it is neither a data-bound nor a key column, just a <td> tag.
So my question is: is there a way to force the rendering to generate an ID anyway, so it can be manipulated from JS?
Thanks in advance,
Jur.
modified 5-Oct-12 10:34am.
|
|
|
|
|
AtALossHere wrote: when this is rendered, no ID is generated sin
ID of what? Every control would have an ID so that you can access it on client side. The snippet shared above has two controls and I see ID defined for both of them.
Please add ID at the time of placing controls and you can access column value associated with that control at runtime.
|
|
|
|
|
No, it isn't... when this is rendered, it just generates [td]Even geduld a.u.b.[/td] (the text assigned design-time) and nothing else...
|
|
|
|
|
Once again, you are expecting an ID for which control?
Grid converted in to a Table and ID's of its rows and columns? If so, why is it needed? You need controls ID's in them.
|
|
|
|
|
I'm expecting ID's for each of the row's labels. These are looked up server-side with
ClientScriptManager cs = Page.ClientScript;
foreach (GridViewRow gvr in GridView2.Rows)
{
Label lb3 = ((Label) gvr.FindControl("PERCS"));
cs.RegisterArrayDeclaration("PERCIDs", String.Concat("'", lb3.ClientID, "'"));
so the JavaScript can edit values with
x = document.getElementById(PERCIDs[i]);
x.value = "Assigned client side";
However, the
Label lb3 = ((Label) gvr.FindControl("PERCS"));
returns null because, browsing the generated page source, no ID is generated.
|
|
|
|
|
Sigh. Finally found the answer. If someone ever has a similar problem and finds this thread, this is what caused it for me.
In another piece of server code (the RowDataBound of the Gridview), this field was explicitly changed.
e.Row.Cells[PercentageKolomNummer].Text = cPercs;
It appears that, by doing this, the rendering "forgets" that it is a label and doesn't generate the ID for the label. By removing the assignment, the ID is generated and FindControl can find it again.
|
|
|
|