Hi,

I have datagrid control which contains textboxs in ItemTemplate. I need to set attribute to the textbox for the purpose of accessing the client side java script function.

Here is my code snip.

<asp:TemplateColumn HeaderText="Fld Date">
                <ItemTemplate>
                    <%# (DataBinder.Eval(Container.DataItem,"DATE","{0:d}").ToString()) %>
                </ItemTemplate>
                <ItemStyle VerticalAlign="Top" />
                <EditItemTemplate>
     <asp:TextBox  id="txtDate" runat="server" /><asp:TextBox id="txtDate" runat="server" />
                </EditItemTemplate>
                <FooterStyle VerticalAlign="Top" />
            </asp:TemplateColumn>

Here i need to set the attribue when edit the datagrid item.
Please help me.

Thanks in advance.

Satees

the datagrid control has an OnItemDataBound event, it fires for each row as it is bound to the grid. Create an event handler for this event, in the handler get a reference to the textbox control and add the attribute.

private void myDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        TextBox textBox =  (TextBox)e.Item.FindControl("txtDate");
                            
        textBox.Attributes.Add("onclick","myJavascriptFunc();");
    }
}

the datagrid control has an OnItemDataBound event, it fires for each row as it is bound to the grid. Create an event handler for this event, in the handler get a reference to the textbox control and add the attribute.

private void myDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        TextBox textBox =  (TextBox)e.Item.FindControl("txtDate");
                            
        textBox.Attributes.Add("onclick","myJavascriptFunc();");
    }
}

Hi,
I have put the same coding that u mentioned above, but it shows an exception like,

" Object reference not set to an instance of an object "

Here is my code:

protected void grdHouseHolds_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
TextBox textBox = (TextBox)e.Item.FindControl("txtDate");
textBox.Attributes.Add("onclick","fnShow();");

LinkButton lButton = (LinkButton)e.Item.FindControl("Delete");
lButton.Attributes.Add("OnClick", "javascript:return confirm('Are you sure, would you like to delete?')");

}
}

But it shows an exception only for textbox control, instead the attribute successfully sets for the linkbutton control.

Any idea how to solve it?

Thanks,
Satees

Is this right? I'm not sure it's good to have two text boxes with the same id in the template column of your datagrid.

<asp:TextBox id="txtDate" runat="server" /><asp:TextBox id="txtDate" runat="server" />

EDIT -> Oh hangon I've just realised the textbox is in the edititem template, I've never used that myself before I need some time to experiment. The If statement in the handler is checking for Item and alternating item. Perhaps this will work:

private void myDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        LinkButton lButton = (LinkButton)e.Item.FindControl("Delete");
lButton.Attributes.Add("OnClick", "javascript:return confirm('Are you sure, would you like to delete?')");
    }
    else if (e.ItemType == ListItem.EditItem)
    {
        TextBox textBox =  (TextBox)e.Item.FindControl("txtDate");
                            
        textBox.Attributes.Add("onclick","myJavascriptFunc();");
    }
        
}

I will try it myself too.

hi, I m not having two textboxes in the editItem template...

I have faced the above exception when tried to set the attribute for the textbox...

Is this right? I'm not sure it's good to have two text boxes with the same id in the template column of your datagrid.

<asp:TextBox id="txtDate" runat="server" /><asp:TextBox id="txtDate" runat="server" />

EDIT -> Oh hangon I've just realised the textbox is in the edititem template, I've never used that myself before I need some time to experiment. The If statement in the handler is checking for Item and alternating item. Perhaps this will work:

private void myDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        LinkButton lButton = (LinkButton)e.Item.FindControl("Delete");
lButton.Attributes.Add("OnClick", "javascript:return confirm('Are you sure, would you like to delete?')");
    }
    else if (e.ItemType == ListItem.EditItem)
    {
        TextBox textBox =  (TextBox)e.Item.FindControl("txtDate");
                            
        textBox.Attributes.Add("onclick","myJavascriptFunc();");
    }
        
}

I will try it myself too.

This article has been dead for over six months. Start a new discussion instead.