我真的需要一些幫助。我試圖在我的GridView中將一個linkbutton連接到ModalPopupExtender,但沒有運氣。基本上我有一個GridView,它列出了數據庫中所有用戶的信息,並且我將用戶名作爲linkbutton。當你點擊用戶名時,應該顯示一個modalpopup,你應該能夠編輯用戶並更新數據庫。我也有一個添加按鈕。當你點擊按鈕時,同樣的modalpopup應該顯示出來,你可以添加一個新的用戶到數據庫。 以下是我的背後的代碼和代碼。到目前爲止,我有兩個主要問題。 (1)OnClick甚至不會一直被解僱。 (2)點擊用戶名linkbutton時,modalpopup不顯示。 真的很感激,如果有人可以幫我在這裏。如何使用ASP.Net C#中的ModalPopupExtender在GridView中連接LinkButton?
這是我的aspx頁面:
<%@ Page Title="" Language="C#" MasterPageFile="~/Default.Master" AutoEventWireup="true" CodeBehind="EditUsers.aspx.cs" Inherits="SPR2_v1.EditUsers" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div id="pagetitle">
<h1>SPR Users</h1>
</div>
<div >
<asp:Label ID="lblErrorMsg" runat="server" ForeColor="Red"></asp:Label>
<table align="center" >
<tr>
<td align="right">
<asp:Button ID="btnAddUser" runat="server" Text="Add User" Width="85px"
onclick="btnAddUser_Click" />
</td>
</tr>
<tr>
<td align="left">
<asp:GridView ID="gvUsers" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False"
DataSourceID="SqlDSUsers" EnableViewState="False">
<Columns>
<asp:TemplateField HeaderText="UserName">
<ItemTemplate>
<asp:LinkButton ID="lnkUserName" runat="server"
Text='<%# Eval("UserName")%>' OnClick="lnkUserName_Click">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UserName" HeaderText="UserName"
SortExpression="UserName" Visible="false" />
<asp:BoundField DataField="Extension" HeaderText="Extension"
SortExpression="Extension" />
<asp:BoundField DataField="Email" HeaderText="E-mail"
SortExpression="Email" />
<asp:BoundField DataField="Company" HeaderText="Company"
SortExpression="Company" />
<asp:BoundField DataField="Department" HeaderText="Department"
SortExpression="Department" />
<asp:BoundField DataField="Access Level" HeaderText="Access Level"
SortExpression="Access Level" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDSUsers" runat="server"
ConnectionString="<%$ ConnectionStrings:SPRConnectionString %>"
SelectCommand="..."></asp:SqlDataSource>
</td>
</tr>
</table>
<asp:ModalPopupExtender id="mpeAddUser" runat="server"
TargetControlID="btnAddUser"
PopupControlID="panelEditUser"
CancelControlID="btnCancel"
PopupDragHandleControlID="PopupHeader"
Drag="true"
DropShadow="true"
BackgroundCssClass="ModalPopupBG" >
</asp:ModalPopupExtender>
<asp:Button ID="btnEditUser" runat="server" style="display:none" />
<asp:ModalPopupExtender id="mpeEditUser" runat="server"
TargetControlID="btnEditUser"
PopupControlID="panelEditUser"
CancelControlID="btnCancel"
PopupDragHandleControlID="PopupHeader"
Drag="true"
DropShadow="true"
BackgroundCssClass="ModalPopupBG" >
</asp:ModalPopupExtender>
<asp:panel id="panelEditUser" style="display: none" runat="server">
<div id="ModalPopup">
<div id="PopupHeader">Add a New User</div>
<table>
<tr>
<td>First Name</td>
<td>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Last Name</td>
<td>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Extension</td>
<td>
<asp:TextBox ID="txtExtension" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>E-mail</td>
<td>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Company</td>
<td>
<asp:DropDownList ID="ddlCompany" runat="server" DataSourceID="SqlDSCompany"
DataTextField="ProductVendorName" DataValueField="ProductVendorID"
AppendDataBoundItems="True">
<asp:ListItem Value="" Text=""></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ...>
</asp:SqlDataSource>
</td>
</tr>
<tr>
<td>Department</td>
<td>
<asp:DropDownList ID="ddlDepartment" runat="server"
DataSourceID="SqlDSDepartment" DataTextField="DepartmentName"
DataValueField="DepartmentID" AppendDataBoundItems="True">
<asp:ListItem Value="" Text=""></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ...>
</asp:SqlDataSource>
</td>
</tr>
<tr>
<td>Access Level</td>
<td>
<asp:DropDownList ID="ddlAccessLevel" runat="server"
DataSourceID="SqlDSAccessLevel" DataTextField="LevelID"
DataValueField="LevelID" AppendDataBoundItems="True">
<asp:ListItem Value="" Text=""></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ...>
</asp:SqlDataSource>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnEdit" runat="server" Text="Submit"
onclick="btnEdit_Click" />
<asp:Button ID="btnReset" runat="server" Text="Reset"
onclientclick="return resetUser();" />
<input id="btnCancel" type="button" value="Cancel" />
</td>
</tr>
</table>
</div></asp:panel>
</div>
</asp:Content>
下面是C#代碼behine,不知何故click事件沒有被解僱所有的時間。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace SPR2_v1
{
public partial class EditUsers : System.Web.UI.Page
{
...
protected void lnkUserName_Click(object sender, System.EventArgs e)
{
LinkButton lbUserName = sender as LinkButton;
GridViewRow gvr = (GridViewRow)lbUserName.NamingContainer;
txtFirstName.Text = "";
txtLastName.Text = "";
txtExtension.Text = gvr.Cells[2].Text;
txtEmail.Text = gvr.Cells[3].Text;
ddlCompany.SelectedItem.Text = gvr.Cells[4].Text;
ddlDepartment.SelectedItem.Text = gvr.Cells[5].Text;
ddlAccessLevel.SelectedItem.Text = gvr.Cells[6].Text;
btnEdit.Text = "Update User";
mpeEditUser.Show();
}
protected void btnAddUser_Click(object sender, EventArgs e)
{
btnEdit.Text = "Add User";
}
}
}
感謝您的回覆。你能告訴我爲什麼onclick甚至不會一直被解僱嗎?但有時它可以。我認爲ModalPopup可能會顯示事件處理程序是否被執行。 – GLP
我發現我的點擊事件不起作用是因爲這些驗證。 – GLP