2017-11-05 13 views
0

私は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に等しい。

私は間違っていますか?

あなたはGridViewでその値を設定しなかった場合は、.CommandArgumentを使用することはできません事前に

+0

'GridView'に' CommandArgument'を設定しないので、デフォルトの空の値があります。 –

+0

私はそこに何を書きますか? –

答えて

0

、ありがとうございました。実際には、CommandArgument属性を設定する必要はありません。したがって、問題は、クリックが発生した行を特定することです。

更新
あなたが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> 

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

を変更する必要があり、その後、すべてが動作するはずです。

//.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 
    } 
    . 
    . 
    . 
} 
+0

動作しませんでした。私はあなたの提案にコードを変更しましたが、GridViewRow selectedRow =((GridViewRow)e.CommandSource)に変更しました。私はコンパイルエラーがあったので。しかし、私はそれが 'System.Web.UI.WebControls.GridView'を 'System.Web.UI.WebControls.GridViewRow'に変換することができないというランタイムエラーが発生しています。 –

+0

@itzickbinder更新を参照 –

関連する問題