0

我是 ASP.NET 的新手

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

GridView 中的标签

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

请帮忙。提前致谢。

4

2 回答 2

1

您需要将 js 行的一部分更改为:

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

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

于 2012-10-11T17:28:47.040 回答
0

改为使用OnRowDataBound。该事件中的文本始终为空,但是当我将其更改为 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);
        }
    }
于 2012-10-11T18:55:49.000 回答