双击数据网格内的链接按钮,它将变为下拉列表
本文关键字:下拉列表 按钮 链接 数据网 数据 网格 双击 | 更新日期: 2023-09-27 18:19:30
我有一个数据网格,看起来像在下
<asp:GridView ID="dgTask" runat="server" Width="100%" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="TaskID" HeaderText="TaskID" ItemStyle-Width="1%" />
<asp:BoundField DataField="TaskName" HeaderText="Task Name" ItemStyle-HorizontalAlign="left"
ItemStyle-Width="10%" />
<asp:BoundField DataField="PriorityName" HeaderText="Priority" ItemStyle-HorizontalAlign="center"
ItemStyle-Width="10%" />
<asp:BoundField DataField="StatusName" HeaderText="Status" ItemStyle-HorizontalAlign="center"
ItemStyle-Width="10%" />
<asp:TemplateField HeaderText="Edit Task" ItemStyle-Width="10%">
<ItemTemplate>
<asp:LinkButton ID="lnkBtnEdit" runat="Server" Text="Edit" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
可以看出,网格中有一个链接按钮控件。我们的要求是,当用户双击该链接按钮时行,则应更改为下拉列表。
如何做到这一点?谷歌搜索没有多大帮助。
请帮助
网格中的数据填充
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var records = GetData();
dgTask.DataSource = records;
dgTask.DataBind();
}
private List<TaskEntity> GetData()
{
List<TaskEntity> lstTaskEntity = new List<TaskEntity>();
Enumerable.Range(1, 10)
.ToList()
.ForEach(
i => lstTaskEntity.Add(new TaskEntity
{
TaskID = i
,
TaskName = string.Concat("TaskName", i)
,
PriorityName = string.Concat("PriorityName", i)
,
StatusName = string.Concat("StatusName", i)
}));
return lstTaskEntity;
}
}
public class TaskEntity
{
public int TaskID { get; set; }
public string TaskName { get; set; }
public string PriorityName { get; set; }
public string StatusName { get; set; }
}
提前感谢
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script language="javascript" type="text/javascript">
function Test(object) {
var splitComponents = object.split(",");
var ddlId = splitComponents[0];
var lnlBtnId = splitComponents[1];
document.getElementById(ddlId).style.display = "inline";
document.getElementById(lnlBtnId).style.display = "none";
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="dgTask" runat="server" Width="100%"
AutoGenerateColumns="False" onrowdatabound="dgTask_RowDataBound"
>
<Columns>
<asp:BoundField DataField="TaskID" HeaderText="TaskID" ItemStyle-Width="1%" />
<asp:BoundField DataField="TaskName" HeaderText="Task Name" ItemStyle-HorizontalAlign="left"
ItemStyle-Width="10%" />
<asp:BoundField DataField="PriorityName" HeaderText="Priority" ItemStyle-HorizontalAlign="center"
ItemStyle-Width="10%" />
<asp:BoundField DataField="StatusName" HeaderText="Status" ItemStyle-HorizontalAlign="center"
ItemStyle-Width="10%" />
<asp:TemplateField HeaderText="Edit Task" ItemStyle-Width="10%">
<ItemTemplate>
<asp:LinkButton ID="lnkBtnEdit" runat="Server" Text="Edit" CommandArgument ='<%# Eval("TaskID") %>' OnClick="Button2_Click"/>
<asp:DropDownList ID = "ddlDropDown" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
行数据绑定
protected void dgTask_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton btn = (LinkButton)e.Row.Cells[4].FindControl("lnkBtnEdit");
DropDownList ddl = (DropDownList)e.Row.Cells[4].FindControl("ddlDropDown");
ddl.DataSource = //give datasource;
ddl.DataBind();
ddl.Attributes.Add("style", "display:none");
btn.Attributes.Add("onclick", "return Test('" + ddl.ClientID + "," + btn.ClientID + "');");
}
}
请参阅以下链接,您可以使用TemplateField和EditItem模板来实现这一点http://www.asp.net/web-forms/tutorials/data-access/custom-formatting/using-templatefields-in-the-gridview-control-cs
试试这个:
为此,你必须将LinkButton和DropDownList添加到项目模板中,并控制这两个的可见性
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton b = (LinkButton)sender;
b.Visible = false;
GridViewRow r = (GridViewRow)b.NamingContainer;
((DropDownList)(GridView1.Rows[r.RowIndex].Cells[0].FindControl("DropDownList1"))).Visible = true;
}
Cell[0]--------------->此处更改控件为的单元格编号