双击datagrid中的链接按钮,它将变为下拉列表

时间:2012-01-03 10:18:23

标签: c# asp.net

我有一个看起来像

的Datagrid
<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; }   
}

提前致谢

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

请参阅以下链接,您可以使用TemplateField和EditItem模板来实现此目的  http://www.asp.net/web-forms/tutorials/data-access/custom-formatting/using-templatefields-in-the-gridview-control-cs

答案 2 :(得分:0)

试试这个:

为此,你必须将LinkBut​​ton和DropDownList添加到itemtemplate并控制两者的可见性

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

单元格[0] -----------&gt;这里更改控件所在的单元格编号