单击按钮后,值将从gridview中的数据中消失

时间:2017-11-05 22:00:15

标签: c# asp.net gridview

我正在尝试使用带有C#的ASP.NET创建一个站点。

我创建了一个gridview,在其中我放置了一个文本框和一个下拉模板字段。

我需要在按下按钮时从文本框中读取文本,并从下拉列表中读取所选值。

当我尝试读取数据时,我只看到文本框的文本为“”,并且无论用户选择了什么,下拉列表的值都是默认值。

这是我的ASP代码:

<asp:GridView ID="grdArticles" runat="server" AutoGenerateColumns="False" OnRowCommand="GrdArticles_RowCommand" Width="1037px" CellPadding="4" ForeColor="#333333" GridLines="None">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:HiddenField ID="hfID" runat="server" Value='<%# Eval("ID") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="title" HeaderText="כותרת" />
        <asp:BoundField DataField="description" HeaderText="תיאור" />
        <asp:BoundField DataField="userName" HeaderText="מעלה המאמר" />
        <asp:BoundField DataField="courseName" HeaderText="קורס" />
        <asp:BoundField DataField="userID" HeaderText="userID" Visible="False" />
        <asp:BoundField DataField="courseID" HeaderText="courseID" Visible="False" />
        <asp:BoundField DataField="facultyID" HeaderText="facultyID" Visible="False" />
        <asp:BoundField DataField="rank" HeaderText="דירוג ממוצע" />
        <asp:TemplateField HeaderText="דירוג משתמש">
            <ItemTemplate>
                <asp:DropDownList runat="server" ID="drpRank">
                    <asp:ListItem Text="1" Value="1"></asp:ListItem>
                    <asp:ListItem Text="2" Value="2"></asp:ListItem>
                    <asp:ListItem Text="3" Value="3"></asp:ListItem>
                    <asp:ListItem Text="4" Value="4"></asp:ListItem>
                    <asp:ListItem Text="5" Value="5"></asp:ListItem>
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="הערות">
            <ItemTemplate>
                <asp:TextBox runat="server" ID="txtComments" TextMode="MultiLine" Rows="3"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:ButtonField Text="דרג" CommandName="rank" />
        <asp:ButtonField Text="הוסף הערות" CommandName="addComments" />
        <asp:ButtonField Text="הורד" CommandName="download" />
        <asp:ButtonField Text="הצג הערות" CommandName="showComments" />
    </Columns>
    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
    <SortedAscendingCellStyle BackColor="#FDF5AC" />
    <SortedAscendingHeaderStyle BackColor="#4D0000" />
    <SortedDescendingCellStyle BackColor="#FCF6C0" />
    <SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>

这是我的C#代码:

protected void GrdArticles_RowCommand(object sender, GridViewCommandEventArgs e)
{
    int index = Convert.ToInt32(e.CommandArgument);
    GridViewRow selectedRow = grdArticles.Rows[index];
    TextBox txt = (TextBox)selectedRow.FindControl("txtComments");
    DropDownList drp = (DropDownList)selectedRow.FindControl("drpRank");
    .
    .
    .
}

当我调试我的代码时,我发现txt.Text等于“”,而drp.SelectedValue等于1。

我做错了什么?

提前谢谢

1 个答案:

答案 0 :(得分:0)

如果您未在.CommandArgument中设置其值,则无法使用GridView。实际上,不需要设置CommandArgument属性。所以问题是确定发生点击的行。

<强>更新
你需要改变

<asp:ButtonField Text="דרג" CommandName="rank" />

templateField

<asp:TemplateField ShowHeader="False">
    <ItemTemplate>
        <asp:LinkButton ID="btnRank" runat="server" 
           CausesValidation="false" 
           CommandName="rank" 
           CommandArgument='<%#Eval("ID")%>' --optional, probably helpful
           Text="Rank"></asp:LinkButton>
    </ItemTemplate>
</asp:TemplateField>

然后一切都应该有用。

//.cs
protected void GrdArticles_RowCommand(object sender, GridViewCommandEventArgs e)
{
    //int index = Convert.ToInt32(e.CommandArgument); //wrong. CommandArgument is not set and is empty by default
    GridViewRow selectedRow = ((Control)e.CommandSource /*that is Button*/).NamingContainer;
       //row is found
    //There are three buttons in each row which should be processed differently
    if(e.CommandName == "rank"){
        //do something
    }
    else if(e.CommandName == "addComment"){
        //addComment
        TextBox txt = (TextBox)selectedRow.FindControl("txtComments");//correct
        DropDownList drp = (DropDownList)selectedRow.FindControl("drpRank");//correct
    }
    .
    .
    .
}