ASP.NET - 检测标签文本以传递变量

时间:2012-10-11 17:24:53

标签: asp.net

我是ASP.NET新手

我尝试做的是捕获lblProposedID字段中的唯一ID(ProposedID),并将其传递给另一个页面。

GridView中的Lable

<asp:TemplateField HeaderText="ID" 
    SortExpression="ID" ItemStyle-Width="5%">
    <ItemTemplate>
    <asp:Label ID="lblProposedID" runat="server" 
        Text='<%#Eval("ProposedID") %>'>
    </asp:Label>
    </ItemTemplate>
    <FooterTemplate>
    </FooterTemplate>
</asp:TemplateField>

背后的代码

private void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    String rColor;
    System.Drawing.ColorConverter colConvert = new ColorConverter();

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        int RowNum = e.Row.RowIndex;
        if (RowNum % 2 == 1)
        {
            rColor = "#FFFFFF";
            //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#DDDDDD'");
        }
        else
        {
            rColor = "#F5F5F5";
            //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='" ++ "'");
        }

        e.Row.BackColor = (System.Drawing.Color)colConvert.ConvertFromString(rColor);
        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='" + rColor + "'");
        e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#00FFFF'");
        e.Row.Attributes.Add("onclick", "window.open('popup.aspx?ProposedID=" + (Label)  e.Row.FindControl("lblProposedID") + 
        "','cal','width=600,height=300,left=270,top=180')"); 
        //e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
    }
}

这是我无法让它工作的路线。

e.Row.Attributes.Add("onclick", "window.open('popup.aspx?textbox={0}" + (Label)e.Row.FindControl("lblProposedID") + 
"','cal','width=600,height=300,left=270,top=180')"); 

请帮忙。提前谢谢。

2 个答案:

答案 0 :(得分:1)

您需要更改js行的部分内容:

((Label)e.Row.FindControl("lblProposedID")).Text

这将从标签中获取实际文本。

答案 1 :(得分:0)

请改用OnRowDataBound。在该事件中,文本始终为null,但是当我将其更改为RowDataBound时,它选择了ID

添加RowDataBound事件

<asp:GridView runat="server" ID="GridView1" 
AutoGenerateColumns="false" OnRowDataBound="GridView1_RowCreated">

<强>代码隐藏

    protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        String rColor;

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int RowNum = e.Row.RowIndex;
            if (RowNum % 2 == 1)
            {
                rColor = "#FFFFFF";
                //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#DDDDDD'");
            }
            else
            {
                rColor = "#F5F5F5";
                //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='" ++ "'");
            }

            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='" + rColor + "'");
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#00FFFF'");
            e.Row.Attributes.Add("onclick", "window.open('popup.aspx?ProposedID=" + ((Label)e.Row.FindControl("lblProposedID")).Text +
            "','cal','width=600,height=300,left=270,top=180')");
            //e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
        }
    }