Datagrid event problem

A

Andrea Williams

I still consider myself to be a novice with ASP.NET, and going through the
samples in my books is not helping me spot the problem. I very often find
my answer after I've posted so maybe it will happen again? :)

I'm having trouble getting an ButtonColumn click event to fire. I set a
break point in the event method and it doesn't break.

here's my Datagrid in the ASPX file:

<asp:datagrid id="dgdSearchResults" runat="server"
OnItemCommand="dgdSearchResults_ItemCommand">
<HeaderStyle CssClass="Title"></HeaderStyle>
</asp:datagrid>

In my code-behind I have this:

protected void dgdSearchResults_ItemCommand(object sender,
DataGridCommandEventArgs e)
{
string strFunctionName = "dgdSearchResults_ItemCommand";
long lngAuthorID = 0;
CISTable.User oUser;
if (e.CommandName == "Select")
{
//An author was selected to be added to the Author hidden element
this.hidAuthorID.Value = e.Item.ItemIndex;
}
}

At design time I add a ButtonColumn to the Datagrid before I bind my
DataSet:

btn.ButtonType = ButtonColumnType.PushButton;
btn.HeaderText = "Select";
btn.CommandName = "Select";
btn.DataTextField = "AuthorID"
btn.Text = "Select";
this.dgdSearchResults.Columns.Add(btn);
btn = null;

That didn't work so I added this to the InitializeComponent() method:
this.dgdSearchResults.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgdSearchResults_
ItemCommand);

Still no luck. All the other buttons, not in a Datagrid are firing the
_click events and running the methods properly, but thie button in the grid
will not. Everything appears correctly in the web browser, but when the
Button is clicked, the dgdSearchResults_ItemCommand is never entered and the
code is not executed. It only goes through Page_Load and not through the
events method.

Can anybody tell me what I'm missing before I go insane???

Andrea
PS: I ran a few searches in the KB articles, but found nothing that would
help.
 
N

Natty Gur

Hi,

Ensure that your dynamic add button is added every time the page is
loading (including postbacks). If you wont create dynamic add controls
they simply don't exist and cant raise any events...

Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
 
A

Andrea Williams

I separated out the code into a test page and the command event fired like
it was supposed to. So I stuck the working code into a User Control to see
if if would still work. (My main code is in a user control and I found a
post where someone thinks that the events don't fire in User Controls.) The
code still works, one theory blown.... So I start adding peices of code
until it broke. When I moved the .DataBind() to the Search button click
event, the DataGrids button quit working. If I leave the Bind in the
Page_Load routine, then the events work. If I don't bind the grid on the
first pass, the events fail to work.

So, my new question is... If I have to bind a datasource to the grid on
every single pass, even when I don't know what my data is going to be, how
do I bind a blank datasource to the grid? Anyone have any ideas?

Andrea
 
A

Andrea Williams

Nevermind... This is not the case after all. Looks like just the creation
of the columns has to be in the page_load every time AND the binding has to
take place in the Page_Load. I had it placed in a button click event before
and that doesn't seem to allow the events to fire.

Thanks for your ideas!

Andrea
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top