简体   繁体   中英

How do I grab values from specific cells in a Gridview via ASP Button ? ASP.NET C#

When I click the ASP Button to fire off my function, it traverses through the gridview, but the values are empty strings. the point of what I am trying to do is allow my users to reorder the gridview via "Drag and Drop" and then all them to save the new order of the items in the gridview. Here is the catch, when they click the button, I want to traverse through the first column in the Gridview to see if any rules were broken in terms of reordering, and if no rules were broken... the sortOrder (Hidden Column) will be updated. Then the new reordering and updated SortOrder values will be passed back to the database table via stored procedure.

So Far I am at the point where I click the button to traverse through the first column of the gridview via nested for loop, and as I traverse, no values are being picked up. They are all being seen as empty strings.

Here is the Gridview ASPX CODE:

  <asp:GridView ID="gridComments" runat="server" BackColor="White" BorderColor="#999999" DataKeyNames="RecNo" OnRowDeleting="gridComments_RowDelete" OnSelectedIndexChanged="gridComments_SelectedIndexChanged"
                        OnRowDataBound="gridComments_RowDataBound" OnRowUpdating="gridComments_RowUpdating" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical" AutoGenerateColumns="False" Width="90%">
                        <FooterStyle BackColor="#CCCCCC" />
                        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                        <AlternatingRowStyle BackColor="#CCCCCC" />
                        <Columns>
                            <asp:TemplateField HeaderText="Edit">
                                <ItemStyle Wrap="false" />
                                <ItemTemplate>
                                    <asp:LinkButton runat="server" ID="lnkEdit" CommandName="select" Text='<%# Eval("Program") %>' CausesValidation="false"></asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Description">
                                <ItemStyle Width="80%" HorizontalAlign="Left" />
                                <ItemTemplate>
                                    <asp:Label runat="server" ID="lblDescription" Text='<%# Eval("Preview") %>'></asp:Label>&nbsp;&nbsp;
                        <asp:LinkButton runat="server" ID="lnkEditComment" Text="Edit"></asp:LinkButton>
                                    <ajaxToolkit:ModalPopupExtender ID="modalPopup" runat="server" PopupControlID="pnlCommentInfo" TargetControlID="lnkEditComment" BackgroundCssClass="modalBackground" CancelControlID="btnCancel"></ajaxToolkit:ModalPopupExtender>
                                    <asp:Panel ID="pnlCommentInfo" runat="server" Style="display: none; text-align: left" CssClass="pnlPop" BorderStyle="Double" Width="500px" ScrollBars="Auto">
                                        <asp:Label runat="server" ID="lblCommentNumber"></asp:Label><br />
                                        <br />
                                        Comment:<br />
                                        <asp:TextBox ID="txtEditComment" runat="server" Rows="6" TextMode="MultiLine" Columns="58" /><br />
                                        <br />
                                        <asp:Button ID="btnSave" runat="server" Text="Save" CausesValidation="false" CommandName="Update" />
                                        <asp:Button ID="btnCancel" runat="server" Text="Cancel" CausesValidation="false" />
                                    </asp:Panel>
                                </ItemTemplate>
                            </asp:TemplateField>

                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:LinkButton runat="server" ID="lnkDelete" CommandName="delete" Text="Delete"></asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>

                            <asp:TemplateField >
                                <HeaderStyle  CssClass =" HiddenCol"/>
                                <ItemStyle CssClass ="HiddenCol" />
                                <ItemTemplate >
                                     <%# Eval("SortOrder") %>

                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView> 

<div style="clear:both">
                        <asp:Button ID="HiddenButton" runat="server" Text="" style="display:none"  BackColor="Transparent" Width="30px"/>
                        <asp:Button ID="Button1" runat="server" OnClick="SaveReorder_Click" Text="Save Ordering Changes" />


                    </div>

                     <asp:Panel ID="StatusPanel" runat="server" CssClass="modalPopup"  Height="123px" Width="292px" style="display:none">
                <div style="text-align: center">
                 <p> ERROR: Please place comments by program in numerical order from least to greatest. Please try again.

                 </p>
            <div style="text-align: center; height: 21px"  >
                    <input id="OK" type="button" value="OK" />
                </div>

                   </div>
        </asp:Panel>

                    <ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" runat="server" BackgroundCssClass="modalBackground" PopupControlID="StatusPanel" OkControlID="OK"  CancelControlID="OK"  TargetControlID="HiddenButton">

                 </ajaxToolkit:ModalPopupExtender>
                </div>

Here is the Function I created in Code Behind File:

I am only at the step where, if they reorder the data wrong then a Popup Appears. if they do it correctly then the updates can be saved to the database after updating the SortOrder Column (Hidden Column)

protected void SaveReorder_Click(object sender, EventArgs e)
    {



        for (int i = 0; i < gridComments.Rows.Count -1; i++ )
        {
               for(int j = i+1; j < gridComments.Columns.Count ; j++)
               {

                   //if (Convert.ToInt32(gridComments.Rows[i].Cells[0].Text) > Convert.ToInt32(gridComments.Rows[j].Cells[0].Text))
                   if (Convert.ToInt32(gridComments.Rows[i].Cells[0].Text) > Convert.ToInt32(gridComments.Rows[j].Cells[0].Text))
                   {
                       ModalPopupExtender1.Show();
                   }

               }
        }


    }```

If you are trying to access the first cell of the row, I think it is expected you would return nothing based on Text because the first column renders a control. That is, unless there is something more in your OnRowDataBound that would be populating the first column more simply.

If you look here: https://stackoverflow.com/a/13795757/2951086 they are looking at pulling the value of the column and are running into issues when trying to access it via Cell.Text of their column using a TemplateField . The linked answer recommends finding the control within the cell like the following to access the Text property (altered to match if you were trying to use your first column's LinkButton's Text field):

    LinkButton lb = (LinkButton)gridComments.Rows[i].Cells[0].FindControl("lnkEdit");
    // use lb.Text as you see fit

Additionally, I am not sure if it is important, but it looks like you are using the column count for j to iterate through a series of rows, which seems questionable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM