adding buttons to a datagrid (web forms)

  • Thread starter Thread starter Phil Townsend
  • Start date Start date
P

Phil Townsend

I have a datagrid which is part of a web form. I need to place a button
in each row to invoke a custom method (not an update, delete, cancel,
etc.). I thought that a button in a datagrid item template raises the
ItemCommand event. however, I can get no response from the button. The
code is never reached. I have registered an event handler that points to
the method. What could I be doing wrong?
 
Phil,

Could you put down your HTML for the grid, as well as the item command event
handler?

We'll see if we can help from there.

Thanks

Dan.
ps. are you related to a Paul Towsend?
 
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.
 
Phil,

From you've setup the command for "showQuestions", but there's no link (in
the code you've provided anyway) that assigns the onclick event to the
function.

try this:
<ItemTemplate>
<asp:Button ID="btnQuestionList" OnClick="showQuestions"
CommandName="showQuestions" Text=" ? " Runat="server" />
</ItemTemplate>

I found this page with an example of the sort of thing you're doing...
http://authors.aspalliance.com/aldotnet/examples/cd.aspx (Example
1)

Hope that helps.
Daniel.
 

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

Back
Top