点击GridView行时,未使用按钮而显示模态弹出窗口失败。

3

当单击gridview行(没有任何按钮)时,我需要显示一个模态弹出框,我已经成功通过点击gridview行来显示弹出框,但不知道如何将值绑定到模态弹出框中显示的文本框中。

  1. 绑定gridview行(当单击时)的值到模态弹出框文本框中
  2. 只有在单击编辑按钮(位于模态弹出窗口底部)时,模态弹出窗口文本框才可编辑
  3. 完成编辑后,单击保存按钮(位于编辑按钮旁边的底部)应保存已编辑的数据

由于我对asp.net还比较陌生,请尽量提供您的建议以满足我的需求。以下代码可以让我在单击gridview行时显示弹出窗口,但请告诉我如何将值绑定到文本框中:

protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GridViewRow row = e.Row;

            if (row.DataItem == null)
            {
                return;
            }

            try
            {
                switch (e.Row.RowType)
                {
                    case DataControlRowType.Header:
                        break;

                    case DataControlRowType.DataRow:
                        e.Row.Attributes.Add("onmouseover", "this.style.cursor='hand'");
                        e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(grid, "Select$" + e.Row.RowIndex.ToString()));
                        e.Row.Attributes.Add("onclick", String.Format("javascript:$find('{0}').show();", ModalPopupExtender2.ClientID));


                        TextBox1.Text = grid.SelectedRow.Cells[0].Text;
                        TextBox2.Text = grid.SelectedRow.Cells[1].Text;
                        TextBox3.Text = grid.SelectedRow.Cells[2].Text;
                        TextBox4.Text = grid.SelectedRow.Cells[3].Text;
                        TextBox5.Text = grid.SelectedRow.Cells[4].Text;

                        ModalPopupExtender2.Show();
                        break;
                }
            }

            catch
            {
                return;
            }
        }

HTML 代码:

<asp:Panel ID="editpanel" runat="server">
            <table width="850px" border="1" class="color">
            <tr>
            <td align="center">
            <asp:Label ID="Label1" runat="server" Text="Firstname" Width="150px"></asp:Label>
            <asp:Label ID="Label2" runat="server" Text="Surname" Width="150px"></asp:Label>
            <asp:Label ID="Label3" runat="server" Text="Visits" Width="150px"></asp:Label>
            <asp:Label ID="Label4" runat="server" Text="$ Speed" Width="150px"></asp:Label>
            <asp:Label ID="Label5" runat="server" Text="Points" Width="150px"></asp:Label>
            <asp:ImageButton id="ImageButton1" runat="server" src="close.png" onclick="close_Click" style="float:right; height: 16px;" ToolTip="To close window"/>
            </td>
            </tr>
            <tr>
            <td>
            <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
            <asp:TextBox ID="TextBox2" runat="server"  ></asp:TextBox>
            <asp:TextBox ID="TextBox3" runat="server" ></asp:TextBox>
            <asp:TextBox ID="TextBox4" runat="server" ></asp:TextBox>
            <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
            </td>
            </tr>
            <tr>
                <td>
                        <asp:ImageButton ID="cancell" runat="server" onclick="cancell_Click"  
                        src="cancel.jpg" style="float:right; width: 16px;" 
                        ToolTip="To cancel member" />                    
                    <asp:ImageButton ID="tickk" runat="server" onclick="tickk_Click" src="tick.jpg" 
                        style="float:right; height: 16px;" ToolTip="To save member" />            
                     <asp:ImageButton ID="Edit" runat="server" onclick="edit_Click"  
                        src="edit.jpg" style="float:right; height: 16px; width: 16px;" 
                        ToolTip="To edit member" />
                </td>
                </tr>
            </table>   
            </asp:Panel>
    <asp:ModalPopupExtender ID="ModalPopupExtender2" runat="server" TargetControlID="modal2" PopupControlID="editpanel" BackgroundCssClass="modalBackground"></asp:ModalPopupExtender>


       <asp:ImageButton ID="modal2" runat="server" src="addmember.jpg" OnClick="modal2_click" Text="modal2" style="display:none;"/> // have created a dummy image button

因为我是 ASP.NET 的新手,请您用您的建议来帮助我。

1个回答

0

使用这个方法通过BindingSource将数据与文本框绑定(仅为示例):

textBoxid.DataBindings.Add(new Binding("Text", customersBindingSource, "ID"));
textBoxname.DataBindings.Add(new Binding("Text", customersBindingSource, "NAME"));
textBoxcity.DataBindings.Add(new Binding("Text", customersBindingSource, "City"));

要使文本框不可编辑,请使用以下代码:

private void EditButton_Click(object sender, EventArgs e)
        {
            if (textBoxid.ReadOnly == true)
            {
                textBoxid.ReadOnly = false;
                //how many text boxes you have, do the same here
            }
            else
            {
                textBoxid.ReadOnly = true;
                //how many text boxes you have, do the same here
            }
        }

为保存您的编辑,请运行一个UPDATE查询并更新所有新数据。


谢谢Shaharyar!!!但是当我在RowDataBound事件中用你的代码替换我的代码后,我遇到了错误(不存在此上下文中的DataBindings和customersBindingSource)。请告诉我customersBindingSource是什么以及它指向哪里?请告诉我我错在哪里以及在哪里粘贴你的代码... - avrmraja

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接