views:

87

answers:

2

I'm trying to use a calendar control to populate a textbox in a gridview cell in edit mode.

The error is with this line

<a href="javascript:OpenPopupPage('Calendar.aspx','<%= txtGvEditTeDate.ClientID %>','<%= Page.IsPostBack %>');">

It doesn't recognize the textbox.

Compilation Error

Compiler Error Message: CS0103: The name 'txtGvEditTeDate' does not exist in the current context

Source Error:

<EditItemTemplate>
        <asp:TextBox ID="txtGvEditTeDate" runat="server"   onfocus="blur();"
             Text='<%# Bind("TimeEntryDate", "{0:d}") %>'>
        </asp:TextBox>

       <a href="javascript:OpenPopupPage('Calendar.aspx','<%= txtGvEditTeDate.ClientID %>','<%= Page.IsPostBack %>');">
       <img src="images/icon-calendar.gif" border="0" align="middle" ></a>
</EditItemTemplate>

Edit

I added runat="server" to the <a> and now I am not getting an error, but when a date is selected in the calendar control is it not being inserted int the txtGvEditTeDate textbox.

A: 

Since the textBox is in the EditItemTemplate you can't access it like that. You have to either get use findcontrol when it is databound or rely on javascript to get the job done.

Mike
+2  A: 

Hey,

Consider making it <a id="r1" runat="server" />

Then, in RowDataBound or RowEditing, do:

var ctl = e.Row.FindControl("txtGvEditTeDate");
var link = e.Row.FindControl("r1");
link.href = "javascript ...";

And assign from the server. Also, you may need to do: e.Row.Cells[] to reference the cell, as row find control sometimes hasn't worked for me.

EDIT: If you are having trouble mapping a date back to the edit control, that's probably because each control is mapped with the gridview's ID appended to it (view source to see what I mean) and as such, the control cannot be referenced by txtGvEditTeDate, so you are going to have to come up with a workaround. One way may be to store the textbox reference in a client-side javascript variable, which can be set in the link href statement potentially. That becomes a little more involved.

Brian
make the `<a>` tag a server control, then you can find it with `FindControl`.
lincolnk
Sorry I don't quite understand. What is "r1"? and what would go after "javascript..."
Joshua Slocum
Oh, formatting error... Will correct, it was supposed to be the link control. Sorry about that.
Brian