文本框和按钮文本属性未在更新面板中的gridviews rowcommand上更新

时间:2018-07-13 09:59:03

标签: c# asp.net bootstrap-modal

我可能很胖,但是由于某些原因,我无法从后面的代码中更新asp:textbox和asp:button的文本。文本框和按钮位于引导程序模式内。我正在调用代码以从gridview的rowcommand事件中更新text属性。

当我单步执行代码时,它会找到文本框和按钮,并说它已更新了文本属性,但是在呈现页面时,文本框中没有文本。我确定我缺少一些简单的东西,但这让我感到困惑。

如果有任何不同,我也会在模式中使用ckeditor。

这是我的rowcommand事件代码:

protected void grdServices_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if(e.CommandName == "EditTreatment")
        {
            int treatmendid;
            if(int.TryParse(e.CommandArgument.ToString(), out treatmendid) == true)
            {
                var x = srvLogic.GetServiceById(treatmendid).AsEnumerable().FirstOrDefault();
                if(x != null)
                {                                      
                    txtNewTreatmentName.Text = x.Field<string>("ServiceName");                        
                    btnAddServ.Text = "Update treatment info";
                }
            }

        }
    }

这是我在aspx页面上的标记:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div class="row">
    <div class="col">

        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <h2 class="text-center">Select treatment category</h2>
                <asp:DropDownList ID="drpServCats" AutoPostBack="true" OnSelectedIndexChanged="drpServCats_SelectedIndexChanged" Font-Bold="true" runat="server" CssClass="form-control d-block mx-auto bg-dark"></asp:DropDownList>

                <h3 class="text-center">Current treatments</h3>



    <asp:GridView ID="grdServices" OnRowCommand="grdServices_RowCommand" AutoGenerateColumns="false" CssClass="table table-borderless table-responsive-md" BorderStyle="None" runat="server" AllowPaging="true" OnPageIndexChanging="grdServices_PageIndexChanging" OnRowDeleting="grdServices_RowDeleting" PagerSettings-Mode="NumericFirstLast">
                        <Columns>
                            <asp:TemplateField HeaderText="Treatment name" AccessibleHeaderText="Treatment name">
                                <ItemTemplate>
                                    <%#Eval("ServiceName") %>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtServiceName" runat="server" Text='<%#Eval("ServiceName") %>' MaxLength="500" CssClass="form-control" TextMode="SingleLine"></asp:TextBox>
                                </EditItemTemplate>

                            </asp:TemplateField>
                            <asp:TemplateField AccessibleHeaderText="Treatment price" HeaderText="Treatment price">
                                <ItemTemplate>
                                    <%#Eval("ServiceCost") %>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtServiceCost" runat="server" Text='<%#Eval("ServiceCost") %>' MaxLength="15" CssClass="form-control" TextMode="SingleLine"></asp:TextBox>
                                </EditItemTemplate>

                            </asp:TemplateField>
                            <asp:TemplateField AccessibleHeaderText="Timely link" HeaderText="Timely link">
                                <ItemTemplate>
                                    <a href='<%# Eval("ServiceTimelyLink") %>' target="_blank"><%# Eval("ServiceTimelyLink") %></a>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtTimely" runat="server" CssClass="form-control" TextMode="SingleLine" MaxLength="2083" Text='<%# Eval("ServiceTimelyLink") %>'></asp:TextBox>
                                </EditItemTemplate>

                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Treatment Short Description" AccessibleHeaderText="Treatment short description">
                                <ItemTemplate>
                                    <%# Eval("ServiceShortDescription") %>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtShortDesc" runat="server" Text='<%# Eval("ServiceShortDescription") %>' CssClass="form-control" MaxLength="500" TextMode="SingleLine"></asp:TextBox>
                                </EditItemTemplate>

                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Treatment detailed description" AccessibleHeaderText="Treatment detailed description">
                                <ItemTemplate>
                                    <%# Eval("ServiceLongDescription") %>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtLongDesc" runat="server" Text='<%# Eval("ServiceLongDescription") %>' CssClass="form-control" TextMode="MultiLine"></asp:TextBox>

                                </EditItemTemplate>

                            </asp:TemplateField>

                            <asp:TemplateField ShowHeader="False">                                
                                <ItemTemplate>
                                    <asp:Button runat="server" Text="Edit" CommandName="EditTreatment" CommandArgument='<%#Eval("ServiceId") %>' ID="LinkButton1"  data-toggle="modal" data-target="#myTreatmentModal"></asp:Button>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
            </asp:GridView>
                    <input type="button" id="btnAddTreatment" value="Add new treatment" class="d-block mx-auto btn-outline-light"  data-toggle="modal" data-target="#myTreatmentModal" />
                </ContentTemplate>                
            </asp:UpdatePanel>
        </div>
    </div>



     <div class="modal text-white bg-dark" id="myTreatmentModal">
    <div class="modal-dialog  modal-lg">
      <div class="modal-content">

        <!-- Modal Header -->
        <div class="modal-header">
          <h4 class="modal-title">Add a new treatment</h4>          
        </div>

        <!-- Modal body -->
        <div class="modal-body">
            <div class="container-fluid">

                  <div class="row">
                      <div class="col">
                          <asp:Label ID="lblNewTreatmentName" runat="server" AssociatedControlID="txtNewTreatmentName" Text="Treatment name"></asp:Label>
                          <asp:RequiredFieldValidator ID="rqNewTreatmentName" runat="server" ErrorMessage="*" ForeColor="Red" ValidationGroup="vldNewTreatment" ControlToValidate="txtNewTreatmentName" Display="Dynamic"></asp:RequiredFieldValidator>
                          <asp:TextBox ID="txtNewTreatmentName" runat="server" CssClass="form-control" TextMode="SingleLine" ValidationGroup="vldNewTreatment"></asp:TextBox>
                          <br />

                          <asp:Label ID="lblNewTreatmentShortDesc" runat="server" AssociatedControlID="txtNewTreatmentShortDesc" Text="Price"></asp:Label>
                          <asp:RequiredFieldValidator ID="rqNewTreatmentShortDesc" runat="server" ErrorMessage="*" ForeColor="Red" ValidationGroup="vldNewTreatment" ControlToValidate="txtNewTreatmentName" Display="Dynamic"></asp:RequiredFieldValidator>
                          <asp:TextBox ID="txtNewTreatmentShortDesc" runat="server" CssClass="form-control" TextMode="SingleLine" ValidationGroup="vldNewTreatment"></asp:TextBox>
                          <br />

                          <asp:Label ID="lblNewTreatmentPrice" runat="server" AssociatedControlID="txtNewTreatmentPrice" Text="Treatment price"></asp:Label>
                          <asp:RequiredFieldValidator ID="rqNewTreatmentPrice" runat="server" ErrorMessage="*" ForeColor="Red" ValidationGroup="vldNewTreatment" ControlToValidate="txtNewTreatmentPrice" Display="Dynamic"></asp:RequiredFieldValidator>
                          <asp:TextBox ID="txtNewTreatmentPrice" runat="server" CssClass="form-control" TextMode="SingleLine" MaxLength="15" ValidationGroup="vldNewTreatment"></asp:TextBox>
                          <br />

                          <asp:Label ID="lblNewTreatmentLink" runat="server" AssociatedControlID="txtNewTreatmentPrice" Text="Timely link"></asp:Label>
                          <asp:RequiredFieldValidator ID="rqNewTreatmentLink" runat="server" ErrorMessage="*" ForeColor="Red" ValidationGroup="vldNewTreatment" ControlToValidate="txtNewTreatmentLink" Display="Dynamic"></asp:RequiredFieldValidator>
                          <asp:TextBox ID="txtNewTreatmentLink" runat="server" CssClass="form-control" TextMode="SingleLine" MaxLength="2083" ValidationGroup="vldNewTreatment"></asp:TextBox>
                          <br />

                          <asp:Label ID="lblNewTreatmentLongDesc" runat="server" AssociatedControlID="txtNewTreatmentLongDesc" Text="Units in Stock"></asp:Label>                           
                          <asp:TextBox ID="txtNewTreatmentLongDesc" runat="server" CssClass="form-control" TextMode="Number" ValidationGroup="vldNewTreatment"></asp:TextBox>
                          <br />


                      </div>
                  </div></div>
        </div>

        <!-- Modal footer -->
        <div class="modal-footer">   
            <div class="container-fluid">
                <div class="row">
                    <div class="col">
                        <asp:Button ID="btnAddServ" runat="server" CssClass="btn-outline-light mx-auto" OnClick="btnAddServ_Click" ValidationGroup="vldNewTreatment" Text="Add new treatment" />
                        <asp:Button ID="btnCancelProdSize" runat="server" Text="Cancel" data-dismiss="modal" CssClass="btn-outline-light mx-auto" />
                    </div>
                </div>                

            </div>         


        </div>

      </div>
    </div>
  </div>
    <script type="text/javascript">
        // enable plugin
        CKEDITOR.plugins.addExternal('base64image', '/ckplugins/base64image-master/', 'plugin.js');
        CKEDITOR.plugins.addExternal('autogrow', '/ckplugins/autogrow/', 'plugin.js');
        CKEDITOR.plugins.addExternal('dialog', '/ckplugins/dialog/', 'plugin.js');
        CKEDITOR.plugins.addExternal('dialogui', '/ckplugins/adialogui/', 'plugin.js');
        CKEDITOR.plugins.addExternal('image2', '/ckplugins/image2/', 'plugin.js');
        CKEDITOR.plugins.addExternal('imageresponsive', '/ckplugins/imageresponsive/', 'plugin.js');
        CKEDITOR.plugins.addExternal('lineutils', '/ckplugins/lineutils/', 'plugin.js');
        CKEDITOR.plugins.addExternal('widget', '/ckplugins/widget/', 'plugin.js');
        CKEDITOR.plugins.addExternal('widgetselection', '/ckplugins/widgetselection/', 'plugin.js');
        CKEDITOR.plugins.addExternal('imageresize', '/ckplugins/imageresize/', 'plugin.js');
        CKEDITOR.plugins.addExternal('table', '/ckplugins/table/', 'plugin.js');
        CKEDITOR.plugins.addExternal('bt_table', '/ckplugins/bt_table/', 'plugin.js');
        CKEDITOR.plugins.addExternal('youtubebootstrap', '/ckplugins/youtubebootstrap/', 'plugin.js');
        CKEDITOR.plugins.addExternal('btgrid', '/ckplugins/btgrid/', 'plugin.js');
        CKEDITOR.plugins.addExternal('templates', '/ckplugins/templates/', 'plugin.js');
        // extraPlugins needs to be set too.
        CKEDITOR.replace('<%= txtNewTreatmentLongDesc.ClientID %>', {
            extraPlugins: 'autogrow,dialog,dialogui,image2,lineutils,widget,widgetselection,imageresize,table,bt_table,youtubebootstrap,btgrid,base64image'
        });
        CKEDITOR.config.removePlugins = 'flash,save,print,forms';
        CKEDITOR.config.skin = 'minimalist,/ckskins/minimalist/';
    </script>

根据要求,这是我的页面加载代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string currentUser = HttpContext.Current.User.Identity.Name.ToString();
        int userrole = userLogic.getCurUserRole(currentUser);
        if (userrole != 1)
        {
            Response.Redirect(@"~/");
        }
        else
        {
            GetCats();
            int ct;
            if(int.TryParse(drpServCats.SelectedValue, out ct) == true)
            {
                GetTreatments(ct);
            }
        }

    }
}

这也是我最初用于获取数据的代码:

protected void GetTreatments(int cat)
{
    Session["CAT"] = cat;
    var dt = srvLogic.GetServicesByCat(cat);
    if(dt != null)
    {            
        grdServices.DataSource = dt;
        grdServices.DataBind();
        dt.Dispose();
    }

}

1 个答案:

答案 0 :(得分:0)

最后我无法解决它,所以我只解决了所有问题,只响应了按钮的click事件:

protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        int treatmendid;
        if (int.TryParse(btn.CommandArgument.ToString(), out treatmendid) == true)
        {
            var x = srvLogic.GetServiceById(treatmendid).AsEnumerable().First();
            if (x != null)
            {
                txtNewTreatmentName.Text = x.Field<string>("ServiceName");
                txtNewTreatmentShortDesc.Text = x.Field<string>("ServiceShortDescription");
                txtNewTreatmentPrice.Text = x.Field<string>("ServiceCost");
                txtNewTreatmentLink.Text = x.Field<string>("ServiceTimelyLink");
                txtNewTreatmentLongDesc.Text = x.Field<string>("ServiceLongDescription");
                btnAddServ.Text = "Update treatment info";
                btnAddServ.ToolTip = x.Field<int>("ServiceId").ToString();
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "success", "$('#myTreatmentModal').modal('show');", true);
            }
        }
    }

这完全没有问题。

相关问题