0

I have a gridview wherein all the controls are in a single templatefield as below:

<asp:GridView ID="GridView1" runat="server" GridLines="None" AutoGenerateColumns="False" BorderWidth="1px" BorderColor="#CCCCCC" DataKeyNames="RegistrationId" Width="476px" DataSourceID="SqlDataSource1" ShowHeader="False" AllowPaging="True" CssClass="auto-style89" OnRowCommand="GridView1_RowCommand">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <table cellpadding="0" cellspacing="0" class="auto-style4">
                                <tr>
                                    <td class="auto-style56" style="vertical-align:top" rowspan="3">
                                        <asp:Image ID="Image1" runat="server" Width="100px" Height="100px" ImageUrl='<%# Bind("LogoPath") %>' style="border: 3px groove #666666; padding: 2px;" />
                                    </td>
                                    <td colspan="2" style="background-color: #B22222">
                                        &nbsp;
                                        <asp:Label ID="lblBusinessName" runat="server" style="font-size: large; color: #FFFFFF; font-weight: 700;" Text='<%# Bind("BusinessName") %>'></asp:Label>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="auto-style81">
                                        &nbsp;<asp:Label ID="lblAddress" runat="server" Text='<%# Bind("StreetAddress") %>' style="color: #6699FF; font-size: small;"></asp:Label>
                                        &nbsp;|
                                        <asp:Label ID="lblCity" runat="server" Text='<%# Bind("City") %>' style="color: #6699FF; font-size: small;"></asp:Label>
                                        &nbsp;-
                                        <asp:Label ID="lblZipCode" runat="server" Text='<%# Bind("ZipCode") %>' style="color: #6699FF; font-size: small;"></asp:Label>
                                        &nbsp;|
                                        <asp:Label ID="lblStates" runat="server" Text='<%# Bind("States") %>' style="color: #6699FF; font-size: small;"></asp:Label>
                                        &nbsp;</td>
                                    <td class="auto-style82">
                                        <asp:Button ID="btnMore" runat="server" CommandName="Select" BackColor="#3366FF" ForeColor="White" Text="More Info" style="border-style: groove; border-color: #666666; padding: 2px; background-color: #808080; font-weight: 700;" />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="auto-style17">&nbsp;<span class="auto-style57">Phone No</span>:
                                        <asp:Label ID="lblContactNo" runat="server" Text='<%# Bind("ContactNo") %>' style="font-size: small"></asp:Label>
                                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;<span class="auto-style57">Website</span>:
                                        <asp:Label ID="lblWebsite" runat="server" Text='<%# Bind("Website") %>' style="font-size: small"></asp:Label>
                                    </td>
                                    <td style="text-align: center" class="auto-style18"><span class="auto-style57"><strong>Reviews</strong></span>(0)</td>
                                </tr>
                            </table>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

Here is the code-behind:

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if(e.CommandName=="Select")
    {
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow selectedRow = GridView1.Rows[index];
        //string BusinessName = selectedRow.FindControl("lblBusinessName").ToString();
        //Session["BusinessName"] = BusinessName;
    }
}

I get the error on the line below:

   int index = Convert.ToInt32(e.CommandArgument);

What seems to be the problem here? Is it because i am using a button instead of a buttonfield.I am just guessing though.I know that the commandarguement is null but how do i correct it?

4

1 回答 1

0

您需要先声明按钮的命令参数,然后才能在代码中实际获取它。像这样更改按钮的声明

<asp:Button ID="btnMore" runat="server" CommandName="Select" BackColor="#3366FF" ForeColor="White" Text="More Info" style="border-style: groove; border-color: #666666; padding: 2px; background-color: #808080; font-weight: 700;" CommandArgument='<%# Bind("RegistrationId") %>' />
于 2014-08-14T12:57:36.133 回答