Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reference dynamic textboxes in a gridview

Status
Not open for further replies.

PushCode

Programmer
Dec 17, 2003
573
US
I have a gridview that displays dynamic rows, each with a textbox for a date. I want to use a javascript popup calendar for each row's textbox. I'm trying to use the ClientID, but I'm getting this error: "error CS0103: The name 'txtRequestDate' does not exist in the current context"

Here's the textbox code:
<asp:TextBox Text='' ID="txtRequestDate" Width="75" runat="server"></asp:TextBox><img src="/_layouts/images/calendar.gif"
alt="Select Date" width="16" height="15" onclick="javascript:NewCal('<%= txtRequestDate.ClientID %>','ddmmmyyyy'); return false;" />

Once rednered, the source input box looks like this:
<input name="ctl00$m$g_d1b32122_5e08_4535_91f1_64358c13bf12$ctl00$gvCheckout$ctl02$txtRequestDate" type="text" id="ctl00_m_g_d1b32122_5e08_4535_91f1_64358c13bf12_ctl00_gvCheckout_ctl02_txtRequestDate" style="width:75px;" />

Apparently the clientid isn't enough to get the unique textbox in each row. Any ideas? Thanks!
 
the textbox doesn't exist at the page level, it exists within the gridview row template. you need to append the javascript in the GridViewRowCreated event.
Code:
if(e.Row.RowIndex < 0) return;
TextBox textbox = (TextBox)e.Row.FindControl("txtRequestDate");
textbox.OnClientClick = "javascript:NewCal('"+textbox.ClientID+"','ddmmmyyyy'); return false;";

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks, I think this is on the right track but I'm getting the following error:

'System.Web.UI.WebControls.TextBox' does not contain a definition for 'OnClientClick' and no extension method 'OnClientClick' accepting a first argument of type 'System.Web.UI.WebControls.TextBox' could be found (are you missing a using directive or an assembly reference?)

And, the click event needs to happen when the user clicks the calendar image next to the textbox...see initial post code. This seems close, but can anyone tell me what's missing?

Here's my code now:
In the gridview, I have this:
<asp:GridView ... OnRowCreated="gvCheckout_RowCreated" ...

And the code behind, this:
protected void gvCheckout_RowCreated(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex < 0) return;
TextBox textbox = (TextBox)e.Row.FindControl("txtRequestDate");
textbox.OnClientClick = "javascript:NewCal('" + textbox.ClientID + "','ddmmmyyyy'); return false;";
}
 
Try:
Textbox.attributes.add("onclick", "javascript:NewCal('" + textbox.ClientID + "','ddmmmyyyy'); return false;";
)
 
Hmmm, traded this line:
textbox.OnClientClick = "javascript:NewCal('" + textbox.ClientID + "','ddmmmyyyy'); return false;";

For this line:
textbox.attributes.add("onclick", "javascript:NewCal('" + textbox.ClientID + "','ddmmmyyyy'); return false;");

Now getting this error:
'System.Web.UI.WebControls.TextBox' does not contain a definition for 'attributes' and no extension method 'attributes' accepting a first argument of type 'System.Web.UI.WebControls.TextBox' could be found (are you missing a using directive or an assembly reference?)

And again, this really should be fired when the user clicks the calendar image, not the textbox. Any ideas?
 
And again, this really should be fired when the user clicks the calendar image, not the textbox. Any ideas?

If you don't want it on the textbox, then why in your original posted code are you trying to add an onlick event to the textbox?

Try adding the attribute to your calendar image.
 
jbenson001, I'm not trying to ad the onclick event to the textbox in my original posted code. I'm trying to ad it to the image right next to the text box...

Here's that same code. The onclick is part of the image, not the text box.

<asp:TextBox Text='' ID="txtRequestDate" Width="75" runat="server"></asp:TextBox><img src="/_layouts/images/calendar.gif"
alt="Select Date" width="16" height="15" onclick="javascript:NewCal('<%= txtRequestDate.ClientID %>','ddmmmyyyy'); return false;" />
 
1. make the img control visible on the server by using an asp:ImageLink. for the navigate url put your js in it's place.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks for the continued input...

I'm not familiar with asp:imagelink. It doesn't appear to be an option. Closest thing I can think of is asp:ImageButton, which I'm trying like this:

<asp:ImageButton ID="calImg" AlternateText="Select Date" runat="server" ImageUrl="/_layouts/images/calendar.gif" OnClientClick="javascript:NewCal('<%= txtRequestDate.ClientID %>','ddmmmyyyy'); return false;" />

But clicking on this image actually appears to submit the page, not popup the calendar. Am I missing something here?

Thanks...
 
ok, then use an asp:hyperlink to wrap an html image tag.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Just add the onclick attribute in code as I said before:
HTML Code:
Code:
<asp:TextBox Text='' ID="txtRequestDate" Width="75" runat="server"></asp:TextBox>\
<img [red]runat="server" id="myImage"[/red] src="/_layouts/images/calendar.gif" alt="Select Date" width="16" height="15" onclick="javascript:NewCal('<%= txtRequestDate.ClientID %>','ddmmmyyyy'); return false;" />
Code-Behind VB code:
Code:
myImage.attributes.add("onclick", "javascript:NewCal('" + myImage.ClientID + "','ddmmmyyyy'); return false;";)
 
Okay, thanks...can someone help me format the javascript call within the asp:hyperlink tag?

<asp:HyperLink runat="server" NavigateUrl='<%# "javascript:NewCal(" & txtRequestDate.ClientID & ", "ddmmmyyyy"); return false;" %>' AlternateText="Select Date" ImageUrl="/_layouts/images/calendar.gif"></asp:HyperLink>

This is throwing all kinds of errors.
 
I figured it out...with Google's help!

This is the working example...no code behind required!

<asp:HyperLink ID="lnkCal" runat="server" NavigateUrl='<%# "javascript:NewCal(&#39;" +((GridViewRow)Container).FindControl("txtRequestDate").ClientID + "&#39;, &#39;ddmmmyyyy&#39;)"%>' ImageUrl="/_layouts/images/calendar.gif" />

Thanks so much to those who offered help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top