954,593 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to set attribute to the controls in datagrid?

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

satees
Newbie Poster
15 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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();");
    }
}
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
 

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

satees
Newbie Poster
15 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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.

hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
 

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.

satees
Newbie Poster
15 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

I recommend this series of articles:

http://aspnet.4guysfromrolla.com/articles/071002-1.2.aspx


And did you try the new code I posted? from your comments above it doesn't look like you read everything properly.

hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
 

Yeah this works for me:

if (e.ItemType == ListItem.EditItem)
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
 

Yes, now i can set the attribute to the datagrid items.
Thanks
hollystyles.

I recommend this series of articles:

http://aspnet.4guysfromrolla.com/articles/071002-1.2.aspx

And did you try the new code I posted? from your comments above it doesn't look like you read everything properly.

satees
Newbie Poster
15 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You