469,948 Members | 2,819 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,948 developers. It's quick & easy.

problem adding buttons to a webforms datagrid

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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
5 1378
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

"Phil Townsend" <ph*******@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #2
Hi,

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

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Phil Townsend" <ph*******@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3
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...

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4
Hi,

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

"Phil Townsend" <ph*******@yahoo.com> wrote in message
news:OK**************@TK2MSFTNGP10.phx.gbl...
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...

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #5
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);
}

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Andrés Giraldo | last post: by
2 posts views Thread by Will | last post: by
3 posts views Thread by kscdavefl | last post: by
reply views Thread by Nikhil Patel | last post: by
2 posts views Thread by saleek | last post: by
1 post views Thread by Joel Byrd | last post: by
reply views Thread by bostjanj | last post: by
1 post views Thread by Paddy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.