gpt4 book ai didi

c# - 双击数据网格中的链接按钮,它将变为下拉列表

转载 作者:太空宇宙 更新时间:2023-11-03 11:28:11 27 4
gpt4 key购买 nike

我有一个如下所示的数据网格

<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>

RowDataBound

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 + "');");
}
}

关于c# - 双击数据网格中的链接按钮,它将变为下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8710727/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com