在页面之间传递ID

时间:2011-02-22 22:00:05

标签: asp.net

我有一个Default.aspx页面,我绑定了一个网格。 Grid中有一个名为Details的按钮。

我还有一个Details.aspx,它有一个GridView。如果我单击Default.aspx中存在的按钮,则会出现一个显示为空的Details.aspx页面。

单击特定行的“详细信息”按钮时,会显示单击该按钮的详细信息。我不了解如何将ID传递给Details.aspx

有人可以指导我吗?

Details.aspx

<asp:GridView ID="DetailsGridView" runat="server" BackColor="White" AutoGenerateColumns="false" 
            BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="ID"
            ForeColor="Black" GridLines="Vertical">
          <FooterStyle BackColor="#CCCC99" />
        <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
        <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" />
        </asp:GridView>

Default.aspx的:                                   

            <Columns>
                <asp:BoundField HeaderText="First Name" DataField="FirstName" />
                <asp:BoundField HeaderText="Last Name" DataField = "LastName" />
                <asp:BoundField HeaderText="HomePhoneNumber" DataField="HomePhoneNumber" />
                <asp:TemplateField HeaderText="ViewDetails">
                <ItemTemplate>
                <asp:Button ID="Deatils" runat="server" Text="Details" />
                </ItemTemplate>    

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Actions">
                <ItemTemplate>
                <asp:Button ID="Modify" runat="server" Text="Modify" />
                <asp:Button ID="Delete" runat="server" Text="Delete" />

                </ItemTemplate>
                </asp:TemplateField>                   
            </Columns>


            <FooterStyle BackColor="#CCCC99" />
            <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
            <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
    </div>

的JavaScript

<script type="text/javascript">
function viewProfile(index)
{
        var GridID = document.getElementById("PersonGridView");
        var row=GridID.rows[parseInt(index)+1];
        window.open('Details.aspx?coaid'+row);
}
</script>

Default.aspx背后的代码:

protected void PersonGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var DataKeys = PersonGridView.DataKeys[e.Row.RowIndex];
                DataKeys.Value.ToString();
                var ID = DataKeys["ID"].ToString();
                Button btnDetails = (Button)e.Row.FindControl("Deatils");
                Button btnModify = (Button)e.Row.FindControl("Modify");
                Button btnDelete = (Button)e.Row.FindControl("Delete");
                btnModify.CommandName = "Modify";
                btnDelete.CommandName = "Delete";
                btnDetails.CommandName = "Deatils";
                btnDelete.CommandArgument = btnModify.CommandArgument = btnDetails.CommandArgument = string.Format("{0}", ID);
                btnDetails.Attributes["onclick"] = string.Format("viewProfile({0}); return false;", e.Row.RowIndex);
            }
        }

2 个答案:

答案 0 :(得分:2)

编辑:我已将我的示例修改为不使用HyperLink,而是构建一个LinkButton,可以导航到您的Details.aspx页面,传递ID值在QueryString中:

<强> Default.aspx的:

<asp:TemplateField>
    <ItemTemplate>
        <asp:LinkButton CommandName="Details" CommandArgument='<%# Eval("ID") %>' Text="Details" runat="server" />
    </ItemTemplate>
</asp:TemplateField>

<强> Default.aspx.cs:

protected void PersonGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Details")
    {
        Server.Transfer("Details.aspx?ID=" + e.CommandArgument.ToString());
    }
}

答案 1 :(得分:0)

单击按钮时在网格上

执行以下操作

public protected sub Button_Click(EventArgs e)
{

if (grid.SelectedItem != null)
{

var selectedRow = grid.SelectedItem ;

// do what ever you want to Selected Row that may be binding it to another Form etc.
}

如果您想以这种方式执行此操作,请对OnSelectedEvent执行相同的操作。

public protected sub OnSelectedEvent_Click(EventArgs e)
{

if (grid.SelectedItem != null)
{

var selectedRow = grid.SelectedItem ;

// do what ever you want to Selected Row that may be binding it to another Form etc.
}