同上,如何在DataGrid中跳转页面,并希望在跳转页面的时候能够得到DataGrid中第一列的值,希望好心人提供一下代码50分献上!

解决方案 »

  1.   

    前台代码:加模板列
    <asp:TemplateField>
         <ItemTemplate>
              <asp:HyperLink ID="HyperLink1" runat="server">HyperLink</asp:HyperLink>
         </ItemTemplate>
    </asp:TemplateField>
    后台代码:protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            foreach (GridViewRow grRow in this.GridView1.Rows)
            {
                HyperLink dr = (HyperLink)grRow.Cells[26].FindControl("HyperLink1");
                if (dr != null)
                {
                    dr.NavigateUrl = "~/Default.aspx?id="+grRow.Cells[0].Text;
                }
            }
        }
      

  2.   

    DataGrid应该有超连接列
    在里面编辑url和要传的参数
      

  3.   

    看不懂饿,不明白什么意思
    希望说的详细一点吧,我希望在DataGrid上添加一个按钮叫更新,当我点击这个按钮的时候可以跳转到另一个页面,并且得到被选择DataGrid中行的ID,谢谢了!分不够的话还可以再加!
      

  4.   

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            foreach (GridViewRow grRow in this.GridView1.Rows)
            {
                HyperLink dr = (HyperLink)grRow.Cells[26].FindControl("HyperLink1");
                if (dr != null)
                {
                    dr.NavigateUrl = "~/Default.aspx?id="+grRow.Cells[0].Text;
                }
            }
        }这个方法不知道是什么意思,有人可以告诉一下吗
      

  5.   

    用Button的话,更改一下就可以了
    前台:这里注意,用到了CommandName属性。
    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Button ID="Button1" runat="server" CommandName="Upda" Text="更新" />
                        </ItemTemplate>
                    </asp:TemplateField>
    后台代码:这样就 可以了。你试试
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            foreach (GridViewRow grRow in this.GridView1.Rows)
            {
                Button bt = (Button)grRow.Cells[27].FindControl("Button1");
                if(bt!=null)
                {
                    bt.CommandArgument=grRow.Cells[0].Text;
                }
                HyperLink dr = (HyperLink)grRow.Cells[26].FindControl("HyperLink1");
                if (dr != null)
                {
                    dr.NavigateUrl = "~/Default.aspx?id="+grRow.Cells[0].Text;
                }
            }
        }protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Upda")
            {
                string url = "~/Default.aspx?id="+e.CommandArgument.ToString();
                Response.Redirect(url);
            }
        }
      

  6.   


    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)//创建GridView的Row事件
    foreach (GridViewRow grRow in this.GridView1.Rows) //遍历GridView的Row
    HyperLink dr = (HyperLink)grRow.Cells[26].FindControl("HyperLink1"); //获取模板列里HyperLink控件
    if (dr != null) //是否获取到HyperLink控件
    dr.NavigateUrl = "~/Default.aspx?id="+grRow.Cells[0].Text; //定义HyperLink的转向Url
      

  7.   

    首先第一饿,在DATAGrid中不能插入
    <asp:TemplateField>
    <ItemTemplate>
    <asp:Button ID="Button1" runat="server" CommandName="Upda" Text="更新" />
    </ItemTemplate>

    这样吧,我把自己的代码拿出来,大家看一下吧
    前台
    <asp:DataGrid id="DataGrid1" runat="server" Width="819px" Height="318px" AutoGenerateColumns="False"
    PageSize="3" AllowPaging="True" DataKeyField="id">
    <Columns>
    <asp:BoundColumn></asp:BoundColumn>
    <asp:BoundColumn DataField="id" HeaderText="项目ID号"></asp:BoundColumn>
    <asp:BoundColumn DataField="business_man" HeaderText="销售员"></asp:BoundColumn>
    <asp:BoundColumn DataField="tac_time" HeaderText="交单时间"></asp:BoundColumn>
    <asp:BoundColumn DataField="contract_time" HeaderText="合同签订时间"></asp:BoundColumn>
    <asp:BoundColumn DataField="achieve_time" HeaderText="完成时间"></asp:BoundColumn>
    <asp:BoundColumn DataField="sun_price" HeaderText="总价格"></asp:BoundColumn>
    <asp:BoundColumn DataField="paid" HeaderText="已付款"></asp:BoundColumn>
    <asp:BoundColumn DataField="balance" HeaderText="余款"></asp:BoundColumn>
    </Columns>
    </asp:DataGrid>
      

  8.   

    我晕,你看清楚我贴的代码撒
    那是GridView的啊
    DataGrid用的话,你改一下再用它啊。郁闷了
      

  9.   

    再给你贴一遍。。
    这次是DataGrid的
    前台代码:
    <asp:TemplateColumn FooterText="更新" HeaderText="更新">
                            <ItemTemplate>
                                <asp:Button ID="Button2" runat="server" CommandName="BtUp" Text="更新" />
                            </ItemTemplate>
                        </asp:TemplateColumn>
    后台代码:
     protected void DataGrid1_ItemCreated(object sender, DataGridItemEventArgs e)
        {
            foreach (DataGridItem gr in this.DataGrid1.Items)
            {
                Button bt = (Button)gr.Cells[2].FindControl("Button1");
                if (bt != null)
                {
                    bt.CommandArgument = gr.Cells[0].Text.ToString();
                }
            }
        }
        protected void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            if (e.CommandName == "BtUp")
            {
                string url = "~/Default.aspx?id=" + e.CommandArgument.ToString();
                Response.Redirect(url);
            }
        }
      

  10.   

    protected void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            if (e.CommandName == "BtUp")
            {
                string url = "~/Default.aspx?id=" + this.DataGrid1.Items[e.Item.ItemIndex].Cells[0].Text;
                Response.Redirect(url);
            }
        }更正一下
    用这个就可以可以把那个ItemCreated事件去掉,这个没用到
      

  11.   

    恩,感谢zsj830120 一遍又一遍的指导我,连我自己也感到了些羞愧,最后谢谢了!
    只可惜分太少了,我还有个问题,又另外开了个帖子,也是关于DataGrid1的,希望你来看一下吧!谢谢!
      

  12.   

    更正一下传值的时候错了一点
    this.DataGrid1.Items[e.Item.ItemIndex].Cells[0].Text;是传不出去的
    只有
    DataGrid1.DataKeys[(int)e.Item.ItemIndex].ToString();  
    可以传出去了
      

  13.   


    怎么传不过去?
    我这里试了是可以传的;
    DataGrid1.DataKeys[(int)e.Item.ItemIndex]这样只可以取DataKeys的值,也就是你设置给DataGrid的DataKey的字段值
    而this.DataGrid1.Items[e.Item.ItemIndex].Cells[0].Text可以取DataGrid所有列的值,模板列转换一下就可以。你说不可以,估计是你哪里写错了。。