为什么在ModalPopUp中选择的值不会在GridView中反映出来? [英] Why the selected value in the ModalPopUp is not reflected and shown in the GridView?

查看:59
本文介绍了为什么在ModalPopUp中选择的值不会在GridView中反映出来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ASP.NET的新开发人员,并且正在为我的公司开发一个基于Web的建议框程序,员工可以在其中提交他们所拥有的任何安全建议.现在,我正在研究此系统的管理"部分.

管理员将能够看到GridView控件中列出的所有建议.在GridView的最后一列中,状态将在此处列出.当管理员单击这些建议之一的状态时,将出现一个新的弹出窗口(asp.net ajax ModalPopUpExtender),其中列出了所有可能的状态,例如:已执行,已批准...等.选择这些状态之一,建议的状态将在数据库中更新. ***我写了代码,但仍然没有更新建议的状态,***

**那么您能帮我修改它吗?**

仅供参考,我具有以下数据库设计:

I am a new ASP.NET developer and I am developing a web-based suggestions box program for my company where the employees can submit any safety suggestions they have. Now, I am working on the Administration part of this system.

The Admin will be able to see all suggestions listed in a GridView control. In the last column of the GridView, the status will be listed there. When the Admin clicks on the status of one of these suggestion, a new pop-up window (asp.net ajax ModalPopUpExtender) will be appeared with listing all the possible status such as: actioned, approved... etc. And when the Admin selects one of these status, the status of the suggestion will be updated in the database. ***I wrote the code but still it doesn''t update the status of the suggestion,***

**so could you please help me in modifying it?**

FYI, I have the following database design:

Employee Table: Username, Name...
    SafetySuggestionsLog: ID, Title, Description, Username, StatusID
    SafetySuggestionsStatus: ID, Status




** ASP.NET代码:**




**ASP.NET code:**

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
                        AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID"
                        width="900px" CssClass="mGrid"
                        DataSourceID="SqlDataSource1"
                        OnRowDataBound="GridView1_RowDataBound">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" CssClass="alt" />
            <HeaderStyle Font-Bold = "True" ForeColor="Black" Height="20px"/>
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
                    ReadOnly="True" SortExpression="ID" />
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                <asp:BoundField DataField="Description" HeaderText="Description"
                    SortExpression="Description" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Username" HeaderText="Username"
                    SortExpression="Username" />
                <asp:BoundField DataField="DivisionShortcut" HeaderText="DivisionShortcut"
                    SortExpression="DivisionShortcut" />
                <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />

                <%-- This to make status be opened and edited through the Ajax ModalPopUp Window --%>
                <asp:TemplateField HeaderText="Status">
                    <ItemTemplate>
                        <asp:LinkButton runat="server" ID="lnkSuggestionStatus" Text=''<%#Eval("Status")%>''
                                        OnClick="lnkSuggestionStatus_Click">
                        </asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>

                <%--<asp:HyperLinkField HeaderText="Status"
                    SortExpression="Status" />--%>
            </Columns>
            <RowStyle HorizontalAlign="Center" />
        </asp:GridView>

        <asp:Button runat="server" ID="btnModalPopUp" style="display:none" />

        <AjaxToolkit:ModalPopUpExtender ID="modalPopUpExtender1"
                                        runat="server"
                                        TargetControlID="btnModalPopUp"
                                        PopupControlID="pnlPopUp"
                                        BackgroundCssClass="popUpStyle"
                                        PopupDragHandleControlID="panelDragHandle"
                                        OkControlID="OKButton">
        </AjaxToolkit:ModalPopUpExtender>

        <asp:Panel runat="server" ID="pnlPopUp">

                    <asp:RadioButtonList ID="StatusList" runat="server" RepeatColumns="1" RepeatDirection="Vertical"
                                            RepeatLayout="Table" TextAlign="Left" DataSourceID="SuggestionStatusDataSource"
                                            DataTextField="Status" DataValueField="ID">
                        <asp:ListItem id="option1" runat="server" Value="ACTIONED" />
                        <asp:ListItem id="option2" runat="server" Value="APPROVED" />
                        <asp:ListItem id="option3" runat="server" Value="PENDING" />
                        <asp:ListItem id="option4" runat="server" Value="TRANSFERRED" />
                    </asp:RadioButtonList>
                    <asp:SqlDataSource ID="SuggestionStatusDataSource" runat="server"
                                        ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
                                        SelectCommand="SELECT * FROM [SafetySuggestionsStatus]"></asp:SqlDataSource>

                    <asp:Button ID="confirmButton" runat="server" Text="Confirm"
                                OnClientClick="javascript:return confirm(''Are you sure you want to send an email notification about the safety suggestion to the owner?'')"
                                OnClick="btnSendStatus_Click" />

            <asp:Button ID="OKButton" runat="server" Text="Close" />
        </asp:Panel>
        </ContentTemplate>
        </asp:UpdatePanel>





**隐藏代码:**





**Code-Behind:**

protected void lnkSuggestionStatus_Click(object sender, EventArgs e)
    {
        LinkButton lnkSuggestionStatus = sender as LinkButton;

        //var safetySuggestionsId =

        //get reference to the row selected
        GridViewRow gvrow = (GridViewRow)lnkSuggestionStatus.NamingContainer;

        //set the selected index to the selected row so that the selected row will be highlighted
        GridView1.SelectedIndex = gvrow.RowIndex;

        //This HiddenField used to store the value of the ID
        HiddenField1.Value = gvrow.RowIndex.ToString();

        //show the modalPopUp
        modalPopUpExtender1.Show();
    }

    public void btnSendStatus_Click(object sender, EventArgs e) {

        var statusID = StatusList.SelectedValue;

        string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";
        //For updating the status of the safety suggestion
        string updateCommand = "UPDATE SafetySuggestionsLog SET StatusID= @statusID where ID=@SafetySuggestionsID";
        using (SqlConnection conn = new SqlConnection(connString))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand(updateCommand, conn))
            {
                cmd.Parameters.AddWithValue("@statusID", Convert.ToInt32(statusID));
                cmd.Parameters.AddWithValue("@SafetySuggestionsID", Convert.ToInt32(HiddenField1.Value));
                cmd.ExecuteNonQuery();
            }
            //reset the value of hiddenfield
            HiddenField1.Value = "-1";
        }

        GridView1.DataBind();

        //SendSuggestionStatusToUser(statusID);
    }



**我没有收到任何错误,但是ModalPopUp窗口中的选定状态没有反映回GridView.为什么?**



**I did not get any error, but the selected status in the ModalPopUp window is not reflected back to the GridView. Why?**

推荐答案

ConnectionStrings:testConnectionString%> SelectCommand ="SELECT * FROM [SafetySuggestionsStatus]"></asp:SqlDataSource> < asp:Button ID ="confirmButton" runat =服务器" Text =确认" OnClientClick ="javascript:return Confirm(``您确定要向所有者发送有关安全建议的电子邮件通知吗?") OnClick ="btnSendStatus_Click"/> < asp:Button ID ="OKButton" runat =服务器" Text =关闭"/> </asp:Panel> </ContentTemplate> </asp:UpdatePanel>
ConnectionStrings:testConnectionString %>" SelectCommand="SELECT * FROM [SafetySuggestionsStatus]"></asp:SqlDataSource> <asp:Button ID="confirmButton" runat="server" Text="Confirm" OnClientClick="javascript:return confirm(''Are you sure you want to send an email notification about the safety suggestion to the owner?'')" OnClick="btnSendStatus_Click" /> <asp:Button ID="OKButton" runat="server" Text="Close" /> </asp:Panel> </ContentTemplate> </asp:UpdatePanel>





**隐藏代码:**





**Code-Behind:**

protected void lnkSuggestionStatus_Click(object sender, EventArgs e)
    {
        LinkButton lnkSuggestionStatus = sender as LinkButton;

        //var safetySuggestionsId =

        //get reference to the row selected
        GridViewRow gvrow = (GridViewRow)lnkSuggestionStatus.NamingContainer;

        //set the selected index to the selected row so that the selected row will be highlighted
        GridView1.SelectedIndex = gvrow.RowIndex;

        //This HiddenField used to store the value of the ID
        HiddenField1.Value = gvrow.RowIndex.ToString();

        //show the modalPopUp
        modalPopUpExtender1.Show();
    }

    public void btnSendStatus_Click(object sender, EventArgs e) {

        var statusID = StatusList.SelectedValue;

        string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";
        //For updating the status of the safety suggestion
        string updateCommand = "UPDATE SafetySuggestionsLog SET StatusID= @statusID where ID=@SafetySuggestionsID";
        using (SqlConnection conn = new SqlConnection(connString))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand(updateCommand, conn))
            {
                cmd.Parameters.AddWithValue("@statusID", Convert.ToInt32(statusID));
                cmd.Parameters.AddWithValue("@SafetySuggestionsID", Convert.ToInt32(HiddenField1.Value));
                cmd.ExecuteNonQuery();
            }
            //reset the value of hiddenfield
            HiddenField1.Value = "-1";
        }

        GridView1.DataBind();

        //SendSuggestionStatusToUser(statusID);
    }



**我没有收到任何错误,但是ModalPopUp窗口中的选定状态没有反映回GridView.为什么?**



**I did not get any error, but the selected status in the ModalPopUp window is not reflected back to the GridView. Why?**


//This HiddenField used to store the value of the ID
HiddenField1.Value = gvrow.RowIndex.ToString();



在上方您指定行索引,而不是数据键(ID值),请在下面尝试



above you assign row index, not the data key(ID value), try below

HiddenField1.Value = GridView1.DataKeys[gvrow.RowIndex].Value.ToString();


这篇关于为什么在ModalPopUp中选择的值不会在GridView中反映出来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆