![](/img/trans.png)
[英]How to get DataKeyNames value of gridview inside LinkButton's click event using c# asp.net
[英]How to hookup the LinkButton in a GridView with a ModalPopupExtender in ASP.Net C#?
我真的需要一些幫助。 我試圖將GridView中的linkbutton連接到ModalPopupExtender,但是沒有運氣。 基本上,我有一個GridView,它列出了數據庫中所有用戶的信息,並且我將用戶名設置為linkbutton。 當您單擊用戶名時,應該會出現一個modalpopup,您應該能夠編輯用戶並更新數據庫。 我也有一個添加按鈕。 當您單擊該按鈕時,應該會出現相同的modalpopup,您可以向數據庫中添加新用戶。 以下是我的aspx和背后的代碼。 到目前為止,我有兩個主要問題。 (1)OnClick甚至不會一直被解雇。 (2)單擊用戶名鏈接按鈕時,不會顯示modalpopup。 非常感謝有人可以在這里幫助我。
這是我的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#中的代碼行,某種程度上,單擊事件並未一直觸發。
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";
}
}
}
要編寫的代碼太多,但是您需要在GridView.RowDataBound
期間將每個LinkButton
與ModalPopupExtender
關聯。
但是,您真正的問題是要訪問要編輯的行數據。 如果這是真正的目標,請參見下面的示例,然后可以將其與ModalPopupExtender
集成。
ASP.NET 2.0的GridView示例:在GridView中編輯基礎數據
我建議您使用ListView
,模板( EditItemTemplate
... etc)更容易使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.