0

I have created two buttons on the same page with rowcommand & rowarguements, however, only one of it work fine, I have no idea why "Unexpected error' box is prompted when I tried to click another button. (btnInterview is working fine, btnContact is not working)

Not just error msg is prompted, btnContact cannot firing the event in rowcommand either.

Code below(.aspx part):

<asp:GridView ID="GridView2" runat="server" AllowPaging="True"
    EmptyDataText="No Sample is found"
    OnPageIndexChanging="GridView1_PageIndexChanging"
    PageSize="20"
    OnSorting="GridView1_Sorting" OnDataBound="GridView1_DataBound"
    OnRowCommand="GridView1_RowCommand" Width="100%" AllowSorting="True" OnRowCreated="GridView1_RowCreated">
    <RowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
    <Columns>
        <asp:TemplateField HeaderText="Phone Interview">
            <ItemTemplate>
                <asp:Button ID="btninterview" runat="server" Text="Interview" CommandName="btninterview_Click"
                    CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" CssClass="intBtn" />
                <asp:HiddenField runat="server" ID="hidid" Value='<%# Bind("Id")%>' />
                <asp:HiddenField runat="server" ID="hidProject" Value='<%# Bind("Project")%>' />
                <asp:HiddenField ID="hidEnablebtn" runat="server" Value='<%# Bind("enableButton")%>' />
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Phone Interview">
            <ItemTemplate>
                <asp:Button ID="btnContact" runat="server" Text="Contact" CommandName="btncontact_Click"
                    CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" CssClass="intBtn" />
                <asp:HiddenField ID="hidVisibleBtn" runat="server" Value='<%# Bind("visibleButton")%>' />
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Code below(c# part):

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e){
    Button btn = (Button)sender;
    switch (btn.CommandName)
    {
        case "btninterview_Click":
            //do sth1
            break;

        case "btncontact_Click":
            //do sth2
            break;
    }            
}
VDWWD
  • 32,238
  • 19
  • 56
  • 70
Edmund
  • 25
  • 6
  • I have found the problem. The problem is due to "CssClass" which is providing another function when button click in javascript and I just forget the code in javascript. – Edmund Feb 28 '18 at 08:26

2 Answers2

0

You are using the RowCommand event. In that case the sender is not the Button, but the GridView itself. So you are getting a Cast exeptions since you are trying to cast a GridView to a Button. You should be casting the CommandSource.

Button btn = e.CommandSource as Button;
VDWWD
  • 32,238
  • 19
  • 56
  • 70
  • still not working. Reminder: btnInterview in my coding is working, only btnContact is not working with message box "Unexcepted error." is prompted. – Edmund Feb 28 '18 at 08:24
0

I hope you can directly get the command name from the GridViewCommandEventArgs itself as below,

switch (**e.CommandName**)
{
    case "btninterview_Click":
        //do sth1
        break;

    case "btncontact_Click":
        //do sth2
        break;
}

REF : GridViewCommandEventArgs

Hope it helps!

Community
  • 1
  • 1
Sivaprasath
  • 390
  • 1
  • 7