0

I am using the following code for date pick field in ASP.NET.

<asp:TemplateField ItemStyle-Width="10%" HeaderText="Date of Birth">
    <EditItemTemplate>
        <br />
        <asp:TextBox ID="text_master_dateedit" runat="server" Text='<%# Bind("MemberDOB", "{0:dd/MM/yyyy}") %>' Width="60%" CssClass="textfoot" ValidationGroup="editing" MaxLength="10" onkeypress="return false;" onkeyup="return false;" oncontextmenu="return false;" onpaste="return false;"></asp:TextBox> &nbsp;
        <a href="javascript:NewCssCal('<%=text_master_dateedit.ClientID %>','DDMMYYYY')"><img id="Img1" alt="Pick a Date" src="Images/cal.png" width="25" height="25" style="vertical-align:middle;" /></a><br />
        <asp:RequiredFieldValidator ID="requireddateedit" runat="server" ValidationGroup="editing" ForeColor="Red" Font-Bold="false" ControlToValidate="text_master_dateedit" ErrorMessage="Date required">*Required</asp:RequiredFieldValidator>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="label_master_dateview" runat="server" Text='<%# Bind("MemberDOB", "{0:dd/MM/yyyy}") %>'></asp:Label>
    </ItemTemplate>
    <FooterTemplate>
        <br />
        <asp:TextBox ID="text_master_datenew" runat="server" Text='<%# Bind("MemberDOB", "{0:dd/MM/yyyy}") %>' Width="60%" CssClass="textfoot" ValidationGroup="adding" MaxLength="10" onkeypress="return false;" onkeyup="return false;" oncontextmenu="return false;" onpaste="return false;"></asp:TextBox> &nbsp;
        <a href="javascript:NewCssCal('<%=text_master_datenew.ClientID %>','DDMMYYYY')"><img id="Img1" alt="Pick a Date" src="Images/cal.png" width="25" height="25" style="vertical-align:middle;" /></a><br />
        <asp:RequiredFieldValidator ID="requireddatenew" runat="server" ValidationGroup="adding" ForeColor="White" Font-Bold="false" ControlToValidate="text_master_datenew" ErrorMessage="Date required">*Required</asp:RequiredFieldValidator>
    </FooterTemplate>
</asp:TemplateField>

I have the text field and date link within the GridView as a TemplateField. When I run the code I get the following error.

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

But the same thing works outside GridView. How can I solve the issue here of identifying the text field ID within GridView? I don't want to use a normal DatePicker since I am using this code for all my projects. Any solution with this same code to make this work?

The NewCssCal function is a JavaScript code that is used for date pick fields in all my projects. It works in all cases except inside GridView.

EDIT

I also tried using the ClientIDMode="Static" and still not working.

In C# i use the below code to access the value of date field since i cannot access it directly when the text field is inside GridView. This works.

protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = (GridViewRow)gv.Rows[e.RowIndex];
    string date = ((TextBox)row.Cells[0].FindControl("text_master_dateedit")).‌​Text;
}

Is there a similar way also to access the text field in this line? I want to make this line work. Is there a way to access the ID of the text field text_master_dateedit (text field inside gridview) instead of '<%=text_master_dateedit.ClientID %>'??

This not works.

<a href="javascript:NewCssCal('<%=text_master_dateedit.ClientID %>','DDMMYYYY')"><img id="Img1" alt="Pick a Date" src="Images/cal.png" width="25" height="25" style="vertical-align:middle;" /></a>

Page Load

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        if (Session["USER"] != null)
        {
            if (!Page.IsPostBack)
            {
                Tab1.CssClass = "Clicked";
                MainView.ActiveViewIndex = 0;
            }
        }
        else
        {
            Response.Redirect("LoginPage.aspx");
        }
    }
    catch
    {
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Error Occured: Try Again!')", true);
    }
}

In Page Load i am only selecting the Tab1 which is the first tab of the aspx page. I have a total of three GridView in the page and three tabs. I am creating the tabs using <asp:MultiView>

Mano Prathibhan C
  • 488
  • 1
  • 11
  • 38
  • 1
    You are obviously accessing an item, that doesn't exist at the current time. Would you show us your code behind? Furthermore: When do you receive that message? During Page Load? At Compile Time? – Marco Aug 09 '17 at 08:04
  • My back end is a JavaScript function `function NewCssCal(pCtrl, pFormat, pScroller, pShowTime, pTimeMode, pHideSeconds)` that gets the selected date and puts that in the text field. This works normally. Only when i put the text field inside a grid view it is not working. My code behind for the `GridView` contains normal fetching of data and binding it. That also works if i use a normal `Date Picker` using `JQuery`. But i want to make this custom date picker work. Code is working fine. My problem is how to give the ID of text field that is inside GridView to the NewCssCal function. – Mano Prathibhan C Aug 09 '17 at 08:12
  • I receive this error during Page Load itself. In C# i use the following code to access this text field. `string date = ((TextBox)row.Cells[0].FindControl("text_master_dateedit")).Text;`. I want to do a similar or another method to pass the text field into the NewCssCal function – Mano Prathibhan C Aug 09 '17 at 08:13
  • well why don't you show us your page load method, when this is where your error occurs? – Marco Aug 09 '17 at 08:20
  • I have added the Page Load code in the question – Mano Prathibhan C Aug 09 '17 at 08:24

1 Answers1

1

Your code shows that :

string date = ((TextBox)row.Cells[0].FindControl("text_master_dateedit")).‌​Text;

what is row here ??

Can you try this instead:

TextBox dateTxt   = (TextBox)GridViewID.Rows[CellIndex].FindControl("text_master_dateedit");
string dateString = dateTxt.Text;

For client side you can find client id of textbox like this :

'<%# ((GridViewRow)Container).FindControl("textDateSent").ClientID %>'

Reference this post

Hope this helps !

Ankit
  • 5,733
  • 2
  • 22
  • 23