【问题标题】:values does not show in my Modal值未显示在我的模态中
【发布时间】:2016-07-04 03:58:56
【问题描述】:

文本框的值没有出现。我的代码有什么问题?模态出现,但它没有得到表的值。我对它做了一个行命令并将代码放在那里。但没有演出。谁能帮帮我。

谢谢!

这是我的 .cs 代码

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int i = Convert.ToInt32(e.CommandArgument);



        GridViewRow gvrow = GridView1.Rows[i];
        txtID.Text = gvrow.Cells[0].Text;
        txtType.Text = gvrow.Cells[1].Text;
        txtModel.Text = gvrow.Cells[2].Text;
        txtQuan.Text = HttpUtility.HtmlDecode(gvrow.Cells[3].Text);
        txtUnit.Text = HttpUtility.HtmlDecode(gvrow.Cells[4].Text);
        txtDate.Text = HttpUtility.HtmlDecode(gvrow.Cells[5].Text);
        txtDesc.Text = HttpUtility.HtmlDecode(gvrow.Cells[6].Text);


        //txtID.Text = GridView1.Rows[i].Cells[0].Text.ToString();


            lblResult.Visible = false;

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(@"<script type='text/javascript'>");
            sb.Append("$('#editModal').modal('show');");
            sb.Append(@"</script>");
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "EditModalScript", sb.ToString(), false);


    }

这是我的模态 div 代码

<div id="editModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
                <h3 id="editModalLabel">View Details</h3>
            </div>    
<asp:UpdatePanel ID="upEdit" runat="server">
    <ContentTemplate>
        <div class ="modal-body">
            <table class="table">
                <tr>
                    <td>Item ID:
                        <asp:TextBox ID="txtID" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>Type:
                        <asp:TextBox ID="txtType" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>Model:
                        <asp:TextBox ID="txtModel" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>Requested Quantity:
                        <asp:TextBox ID="txtQuan" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>Unit:
                        <asp:TextBox ID="txtUnit" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>Date Needed:
                        <asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>Description:
                        <asp:TextBox ID="txtDesc" runat="server"></asp:TextBox>
                    </td>
                </tr>
            </table>
        </div>
        <div class="modal-footer">
            <asp:Label ID="lblResult" Visible="false" runat="server"></asp:Label>
            <asp:Button ID ="btnSave" runat="server" Text="Approve" CssClass="btn btn-primary" OnClick="btnSave_Click" />
            <button class="btn btn-info" data-dismiss="modal" aria-hidden="true">Close</button>
        </div>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="RowCommand" />
        <asp:AsyncPostBackTrigger ControlID ="btnSave" EventName="Click" />
    </Triggers>
</asp:UpdatePanel>
    </div>
        </div>
    </div>

我的 GRIDVIEW 的标记 1

<asp:UpdatePanel ID="panel1" runat="server">
     <ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="TransactionID" OnRowDataBound="GridView1_OnRowDataBound" OnRowCommand="GridView1_RowCommand" CellPadding="4" AllowPaging="true" PageIndex="2" OnPageIndexChanging="GridView1_PageIndexChanging" HeaderStyle-BackColor ="CornflowerBlue" BorderWidth="1" BorderColor="Gray" Width="100%" CssClass=" table table-hover" >
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="chkSelect" runat="server"  />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <img style="cursor:pointer" src ="../Images/Icons/plus2.png" />
                <asp:Panel ID ="pnlDetails" runat="server" Style="display: none">
                    <asp:GridView ID="gvDet" runat="server" AutoGenerateColumns="false" CssClass="ChildGrid">
                        <Columns>
                            <%--<asp:BoundField ItemStyle-Width="20px" DataField="ID" HeaderText="ID" />--%>
                            <asp:BoundField ItemStyle-Width="200px" DataField="ItemType" HeaderText="Type" />
                            <asp:BoundField ItemStyle-Width="250px" DataField="ItemModel" HeaderText="Model" />
                            <asp:BoundField ItemStyle-Width="140px" DataField="ItemQuantity" HeaderText="Requested Quantity" />
                            <asp:BoundField ItemStyle-Width="80px" DataField="ItemUnit" HeaderText="Unit" />
                            <asp:BoundField ItemStyle-Width="100px" DataField="ItemDate" HeaderText="Date Needed" />
                            <asp:BoundField ItemStyle-Width="200px" DataField="ItemDesc" HeaderText="Description" />
                            <%--<asp:BoundField ItemStyle-Width="80px" DataField="ItemStatus" HeaderText="Status" />--%>
                        </Columns>
                    </asp:GridView>
                </asp:Panel>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Transaction Number" HeaderStyle-ForeColor="White">
            <ItemTemplate>
                <asp:Label ID ="lblmosID" runat="server" Text='<%#Bind ("TransactionID") %>'></asp:Label>
            </ItemTemplate>
            <ItemStyle Width="30px" Font-Size="15px" Font-Names="Calibri" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Date" HeaderStyle-ForeColor="White">
            <ItemTemplate>
                <asp:Label ID ="lblDate" runat="server" Text='<%#Bind ("DateFiled") %>'></asp:Label>
            </ItemTemplate>
            <ItemStyle Width="130px" Font-Names="Calibri" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name" HeaderStyle-ForeColor="White">
            <ItemTemplate>
                <asp:Label ID ="lblName" runat="server" Text='<%#Bind ("ReqName") %>'></asp:Label>
            </ItemTemplate>
            <ItemStyle Font-Names="Calibri" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Company" HeaderStyle-ForeColor="White">
            <ItemTemplate>
                <asp:Label ID ="lblComp" runat="server" Text='<%#Bind ("ReqCompany") %>'></asp:Label>
            </ItemTemplate>
            <ItemStyle Font-Names="Calibri" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Branch" HeaderStyle-ForeColor="White">
            <ItemTemplate>
                <asp:Label ID ="lblBranch" runat="server" Text='<%#Bind ("ReqBranch") %>'></asp:Label>
            </ItemTemplate>
            <ItemStyle Font-Names ="Calibri" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Business Unit" HeaderStyle-ForeColor="White">
            <ItemTemplate>
                <asp:Label ID ="lblBU" runat="server" Text='<%#Bind ("ReqBU") %>'></asp:Label>
            </ItemTemplate>
            <ItemStyle Font-Names="Calibri" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Department" HeaderStyle-ForeColor="White">
            <ItemTemplate>
                <asp:Label ID ="lblDept" runat="server" Text='<%#Bind ("ReqDept") %>'></asp:Label>
            </ItemTemplate>
            <ItemStyle Font-Names="Calibri" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Section" HeaderStyle-ForeColor="White">
            <ItemTemplate>
                <asp:Label ID ="lblsection" runat="server" Text='<%#Bind ("ReqSection") %>'></asp:Label>
            </ItemTemplate>
            <ItemStyle Font-Names="Calibri" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Status" HeaderStyle-ForeColor="White">
            <ItemTemplate>
                <asp:Label ID ="lblStatus" runat="server" Text='<%#Bind ("TransStatus") %>'></asp:Label>
            </ItemTemplate>
            <ItemStyle Font-Names="Calibri" />
        </asp:TemplateField>
        <%--<asp:TemplateField HeaderText="">
            <ItemTemplate>
                <asp:LinkButton ID ="lnkEdit" runat="server" Text="View" PostBackUrl='<%# "Details.aspx?Id=" + Eval("TransactionID") %>'></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>--%>
        <asp:ButtonField CommandName="editRecord" ControlStyle-CssClass="btn btn-info" ButtonType="Button" Text="View" HeaderText="View Request" HeaderStyle-ForeColor="White">
        <ControlStyle CssClass ="btn btn-info" />    
        </asp:ButtonField>
    </Columns>
    <HeaderStyle BackColor="CornflowerBlue" />
</asp:GridView>
         </ContentTemplate>
     <Triggers>
         <asp:AsyncPostBackTrigger ControlID="GridView1" />
     </Triggers>
     </asp:UpdatePanel>

我的代码有什么问题?

谢谢!

【问题讨论】:

  • 尝试对文本框使用 AutoPostBack="true"
  • 请张贴您的 GridView 控件的标记
  • 张贴在丹尼斯下面
  • 嗨,丹尼斯,更新了我的问题。谢谢
  • 谢谢Jedi。看看我发布的答案。让我们看看它是否对你有帮助

标签: c# jquery asp.net gridview bootstrap-modal


【解决方案1】:

我已尝试在我这边重新创建您的问题,但它似乎正在工作。我的代码中唯一的主要区别是我在 GridView 上使用了 AutoGenerateSelectButton="True" 和在 ScriptManager 上使用了 EnableCdn="true"控制。看看下面的代码,看看你是否能发现你的逻辑没有做的一些奇怪的事情,并找出你的逻辑不起作用的原因。我希望它对你有所帮助。

背后的代码:

public partial class DisplayGridViewRowInPopup : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GetData();
        }
    }

    public void GetData()
    {
        var p1 = new Product { TransactionID = 1, ItemType = "Type1", ItemModel = "Model1", ItemQuantity = 1, ItemUnit = "Unit1", ItemDate = DateTime.Now, ItemDesc = "Product 1" };
        var p2 = new Product { TransactionID = 2, ItemType = "Type2", ItemModel = "Model2", ItemQuantity = 2, ItemUnit = "Unit2", ItemDate = DateTime.Now, ItemDesc = "Product 2" };
        GridView1.DataSource = new List<Product> { p1, p2 };
        GridView1.DataBind();
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int i = Convert.ToInt32(e.CommandArgument);

        GridViewRow gvrow = GridView1.Rows[i];
        txtID.Text = gvrow.Cells[1].Text;
        txtType.Text = gvrow.Cells[2].Text;
        txtModel.Text = gvrow.Cells[3].Text;
        txtQuan.Text = HttpUtility.HtmlDecode(gvrow.Cells[4].Text);
        txtUnit.Text = HttpUtility.HtmlDecode(gvrow.Cells[5].Text);
        txtDate.Text = HttpUtility.HtmlDecode(gvrow.Cells[6].Text);
        txtDesc.Text = HttpUtility.HtmlDecode(gvrow.Cells[7].Text);

        lblResult.Visible = false;
        string script = "<script type='text/javascript'>$('#editModal').modal('show');</script>";
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "EditModalScript", script, false);
    }
}

public class Product
{
    public int TransactionID { get; set; }
    public string ItemType { get; set; }
    public string ItemModel { get; set; }
    public int ItemQuantity { get; set; }
    public string ItemUnit { get; set; }
    public DateTime ItemDate { get; set; }
    public string ItemDesc { get; set; }
}

.ASPX:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnableCdn="true"></asp:ScriptManager>
        <div id="editModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
                        <h3 id="editModalLabel">View Details</h3>
                    </div>
                    <asp:UpdatePanel ID="upEdit" runat="server">
                        <ContentTemplate>
                            <div class="modal-body">
                                <table class="table">
                                    <tr>
                                        <td>Item ID:
                        <asp:TextBox ID="txtID" runat="server"></asp:TextBox>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>Type:
                        <asp:TextBox ID="txtType" runat="server"></asp:TextBox>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>Model:
                        <asp:TextBox ID="txtModel" runat="server"></asp:TextBox>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>Requested Quantity:
                        <asp:TextBox ID="txtQuan" runat="server"></asp:TextBox>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>Unit:
                        <asp:TextBox ID="txtUnit" runat="server"></asp:TextBox>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>Date Needed:
                        <asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>Description:
                        <asp:TextBox ID="txtDesc" runat="server"></asp:TextBox>
                                        </td>
                                    </tr>
                                </table>
                            </div>
                            <div class="modal-footer">
                                <asp:Label ID="lblResult" Visible="false" runat="server"></asp:Label>
                                <asp:Button ID="btnSave" runat="server" Text="Approve" CssClass="btn btn-primary" />
                                <button class="btn btn-info" data-dismiss="modal" aria-hidden="true">Close</button>
                            </div>
                        </ContentTemplate>
                        <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="RowCommand" />
                            <asp:AsyncPostBackTrigger ControlID="btnSave" EventName="Click" />
                        </Triggers>
                    </asp:UpdatePanel>
                </div>
            </div>
        </div>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand" AutoGenerateSelectButton="True">
                </asp:GridView>
            </ContentTemplate>
            <Triggers>
             <asp:AsyncPostBackTrigger ControlID="GridView1" />
         </Triggers>
        </asp:UpdatePanel>
    </form>
</body>
</html>

输出:

【讨论】:

  • 嗨,丹尼斯,它对您有用,可能是因为您的数据是预加载的?我的数据来自数据库,我绑定它
  • 没关系,只要确保使用 if(!Page.IsPostBack) 绑定一次数据即可。绑定到数据库或静态数据列表没有区别在这种情况下
  • 我可以把我的项目发给你吗?如果你没问题
  • 好的,把你的数据库也发给我 - denis.wessels7@gmail.com
  • 嗨,丹尼斯,刚刚发送。
猜你喜欢
  • 1970-01-01
  • 2018-04-16
  • 2022-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-28
相关资源
最近更新 更多