1

My question seems to be a little hard to understand but is pretty simple and I'm sure the solution is pretty simple too.

I have one Gridview that can Edit and Update. We all know that when we click on edit if the column is setted to ReadOnly = "True" a default Textbox control will appear...

I can return values from that Textbox's dynamically created by click on Edit button of Gridview, that's all good...

My question is: I want add the javascript code to that dynamically textbox created when the edit button is clicked inside the gridview.

This is the javascript code to a Datepicker show when a textbox is clicked:

<head runat="server">

<link rel="Stylesheet" type="text/css" href="css/jquery.datepick.css" />
<script type="text/javascript" src="js/jquery.datepick.js"></script>

<script type="text/javascript" language="javascript">
    $(function () {    
        $('#Textbox_Name').datepick({ dateFormat: 'dd/mm/yyyy' });            
    });
</script>
</head>

Now, Instead of put the textbox name on that code first i want know the name of my gridview edit textbox dynamically created and then show the datepicker on that textbox

3 Answers 3

1

Handle the RowCreated event:

protected void grid_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowState == DataControlRowState.Edit)
    {
        var textBox = e.Row.FindControl("myTextBox") as TextBox;
        ClientScript.RegisterStartupScript(this.GetType(), "datepick", 
        "$(function () { $('#" + textBox.ClientID + "').datepick({ dateFormat: 'dd/mm/yyyy' });  })", true);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much Amiram Korach, you made my day! Solved
@B1GB0Y Accept and vote for the answer that help you, especial if make it work.
1

The easy way here is to add a css class name to your editor and then applies the datepick to all editors that have this class as

<script type="text/javascript" language="javascript">
    $(function () {
        $('.className').datepick({ dateFormat: 'dd/mm/yyyy' });
    });
</script>

See also this question that have a solution on that including the update panel: Asp.Net UpdatePanel in Gridview Jquery DatePicker

Comments

0

I got mine to work like this:

(RowDataBound event)

if (e.Row.RowType == DataControlRowType.DataRow)
{
   TextBox tbOne = e.Row.FindControl("txtMyTextBox") as TextBox;
   if (tbOne != null)
   {
       string js = "$(function() { $('#" + tbOne.ClientID + "').datepicker();  });";
       ClientScript.RegisterStartupScript(this.GetType(), "DatePickJS_" + Guid.NewGuid().ToString("N"), js, true);
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.