problem adding buttons to a webforms datagrid

P

Phil Townsend

I need to add a button to a datagrid. I have tried using the
ButtonColumn and have also tried adding a button to a templatecolumn >
itemtemplate. Whatever I have tried doesn't work, nor does it produce
errors. The method i have specified to execute is never reached. Can
anybody tell what I am doing wrong? Thanks...

webform code:

<form id="Form1" method="post" runat="server">
<asp:datagrid id="dgconcept" runat="server"
AlternatingItemStyle-BackColor="#99ffcc" AutoGenerateColumns="False"
CellPadding="4">
<Columns>
<asp:TemplateColumn HeaderText="">
<ItemTemplate>

<%#DataBinder.Eval(Container.DataItem,"conceptid")%>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Concept">
<ItemTemplate>

<%#DataBinder.Eval(Container.DataItem,"concept")%>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Button ID="btnQuestionList"
CommandName="showQuestions"
Text=" ? " Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
<asp:datagrid id="dgquestions"
runat="server"></asp:datagrid>
<asp:datagrid id="dganswers" runat="server"></asp:datagrid>
</form>

Codebehind:


protected void showQuestions(object source,
DataGridCommandEventArgs
e)
{
dgquestions.DataSource=dsfulltest().Tables[1];
dgquestions.Visible=true;
dgquestions.DataBind();
}

As mentioned before, when running this through a VS.NET debugger, the
code in showQuestions method is never reached. I have tried this using
the buttoncolumn instead, but with the same results--the code cannot be
reached.
 
A

Amit

Its possible that the event wiring between the event and the handler
ShowQuestions is broken. Visual Studio does that sometimes. Just open up the
InitializeComponent method and see if the event wiring code is in there.

The other thing I noticed is that you seem to be binding data to the
dgquestions grid instead of the dgconcept grid which has all the controls :)
-amit
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Replace CommandName by OnCommand in the button declaration:
<asp:Button ID="btnQuestionList"
OnCommand="showQuestions"
Text=" ? " Runat="server" />

Cheers,
 
P

Phil Townsend

Thanks for all the replies, but unfortunately nothing seems to work. I
have tried using an ItemTemplate. I have tried using a ButtonColumn. I
have tried with and without registering an event handler in codebehind.
I have using OnCommand & OnClick (within the asp:button or
buttoncolumn), and OnItemCommand (within the datagrid itself), and still
the method I am referencing is never reached. Should I abandon attempts
at trying to use a button from within a datagrid? I have found that the
only buttons that work within datagrids are the standard Edit, Update,
Delete and Cancel. Is the datagrid even capable of issuing an action to
a custom method? One would certainly think so, but it appears not so...
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Did you make sure that you have runat=server in the button?
 
P

Phil Townsend

Yes, I have runat=server in the button tag. It is as if there is no
wiring to the event handler, even though I have specified it in
InitializeComponent...

private void InitializeComponent()
{
this.dgconcept.ItemCommand+=new
DataGridCommandEventHandler(this.showQuestions);
this.Load+=new System.EventHandler(this.Page_Load);
}
 

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