开发者

How to access controls in a list view row

I have listview and every row has a Drop Down List and a hyperlink control.

What i'm trying to do is change the navigate url for the hyperlink based on the selection of the drop down list. How would I get the row that the control posted back from so I can find the 开发者_JS百科hyperlink control?


You may try the following:

<%@ Page Language="C#" %>
<script type="text/C#" runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            MyList.DataSource = Enumerable.Range(1, 5);
            MyList.DataBind();
        }
    }

    protected void DDLChange(object sender, EventArgs e)
    {
        var ddl = (DropDownList)sender;
        var link = (HyperLink)ddl.Parent.FindControl("MyLink");
        link.NavigateUrl = ddl.SelectedValue;
        link.Text = ddl.SelectedValue;
    }
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="Form1" runat="server">
        <asp:ListView ID="MyList" runat="server">
            <ItemTemplate>
                <div>
                    <asp:DropDownList runat="server" AutoPostBack="true" OnSelectedIndexChanged="DDLChange">
                        <asp:ListItem Value="http://www.google.com" Text="http://www.google.com" />
                        <asp:ListItem Value="http://www.bing.com" Text="http://www.bing.com" />
                        <asp:ListItem Value="http://www.yahoo.com" Text="http://www.yahoo.com" />
                    </asp:DropDownList>
                    <asp:HyperLink runat="server" ID="MyLink" NavigateUrl="http://www.google.com" Text="http://www.google.com" />
                </div>
            </ItemTemplate>
        </asp:ListView>
    </form>
</body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜