Входная строка была в неправильном формате в строке

У меня есть gridview, где все элементы управления находятся в одном поле шаблона, как показано ниже:

<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>

Вот код позади:

 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;
    }
}

Я получаю сообщение об ошибке в строке ниже:

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

В чем здесь проблема? Это потому, что я использую кнопку вместо поля кнопок. Я просто догадываюсь. Я знаю, что аргумент команды нулевой, но как мне исправить это?

1 ответ

Вам нужно объявить аргумент команды для кнопки, прежде чем фактически получить ее в коде. Измените объявление кнопки, как это

<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") %>' />
Другие вопросы по тегам