Mano Prathibhan C
Mano Prathibhan C

Reputation: 518

Custom DatePicker not working inside GridView

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>

Upvotes: 0

Views: 424

Answers (1)

Ankit
Ankit

Reputation: 6133

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 !

Upvotes: 1

Related Questions