我在GridView中建了一个TemplateField字段,在编辑摸版的时候放了一个Button我先点这个Button弹出另一个界面,在这个界面中修改这条纪录,问题出来了,1。我如何取得我选择的这条纪录的ID 2。这个WEB的ID如何传到另一个编辑WEB中去 3。在另一个编辑WEB中如何提取这条纪录的其他数据,从而附值到另一个编辑WEB的控件中。就这三个问题,麻烦大家帮我写写代码,因为刚学ASP。NET 很多东西不知道怎么写,以前是搞DELPHI的,现在想转行,给些代码 谢谢大家!

解决方案 »

  1.   


     <asp:GridView Width="100%" ID="GridView1" runat="server" CssClass="GridCss" AutoGenerateColumns="false"
                    PageSize="20" AllowPaging="true" OnRowCommand="GridView1_RowCommand" OnPageIndexChanging="GridView1_PageIndexChanging">
                    <Columns>
                        <asp:ButtonField Text="SingleClick" CommandName="SingleClick" Visible="False" />
                            <asp:TemplateField HeaderText="费用代号">
                            <ItemTemplate>
                                <%#Eval("fydh")%>
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Left" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="费用名称">
                            <ItemTemplate>
                                <%#Eval("fymc")%>
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Left" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="确定">
                            <ItemTemplate>
                                <asp:LinkButton ID="deleteButton" runat="server" Text="删除" CommandArgument='<%#Eval("identifierid")%>'
                                    CommandName="Delete_F" OnClientClick="return confirm('您确认删除吗?')" />
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Right" />
                        </asp:TemplateField>
                    </Columns>
                    <SelectedRowStyle CssClass="SelectCss" />
                    <AlternatingRowStyle CssClass="AlterRowCss" Height="22px" />
                    <RowStyle CssClass="RowCss" Height="22px" />
                    <HeaderStyle CssClass="HeadCss" Height="22px" />
                    <FooterStyle CssClass="FootCss" />
                    <PagerStyle CssClass="PageCss" />
                </asp:GridView>
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToString() == "Delete_F")
            {
                MaterialBF db = new MaterialBF();
                string identifierid = e.CommandArgument.ToString();//取出要更新信息的唯一标识            db.DeleteMyFy(identifierid);
                BindData();
                       }
        }
      

  2.   

    1. button中有CommangArgument属性 你在绑定数据的时候可以把主键绑定到这个属性上去   再写一个CommandName属性用于指定是做什么操作的 有Delete、Update、Edit
     在后台通过e.CommangArgument获得主键ID你可以通过ID重新查询一遍数据然后把值添加到控件上
     
      

  3.   

    mylibin
    按你说的方法去做 我如果把这个BUTTON绑定了ID的话 他的TEXT就显示的是ID了 我只想让他显示编辑这俩个字~
    你说的 通过ID重新查询一遍数据然后把值添加到控件上  这个方法可用,主要是我怎么把ID弄过来 能不能给稍微写写代码~
      

  4.   

    GridView添加下面屬性,假設ID是GridView綁定數據中唯一標識
    DataKeyNames="ID"
    當button觸發事件
        protected void Button1_OnClick(object sender, EventArgs e)
        {
            Response.Redirect("xxx.aspx?ID=" + GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString());
        }
    xxx.aspx.cs接收ID
        protected void Page_Load(object sender, EventArgs e)
        {
            string ID = Request.QueryString["ID"];
        }
      

  5.   

    protected void LinkButton1_Click1(object sender, EventArgs e)
        {
            Response.Redirect("http://localhost:4552/WebSite1/Default3.aspx?ID=" + GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString()); 
        }
    我按shinevi这样写了 他的思路很清晰,但是报错
    索引超出范围。必须为非负值并小于集合大小。
    参数名: index
    这是为什么呢~
      

  6.   


                     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="466px" 
                        DataKeyNames="ID" OnRowEditing="GridView1_Editing" >
                         <Columns>
                             ...(各個欄位)
                             <asp:CommandField ShowEditButton="True">
                                 <HeaderStyle Width="60px" />
                                 <ItemStyle Width="60px" />
                             </asp:CommandField>
                         </Columns>
                       </asp:GridView>
        protected void GridView1_Editing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            Response.Redirect("xxx.aspx?Vote_ID=" + GridView1.DataKeys[GridView1.EditIndex].Value.ToString());
        }
      

  7.   

    不好意思~修正一下~protected void GridView1_Editing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            Response.Redirect("xxx.aspx?ID=" + GridView1.DataKeys[GridView1.EditIndex].Value.ToString());
        }
      

  8.   

    一楼兄弟的答案很明确了。只不是他是删除,你是修改<asp:LinkButton ID="deleteButton" runat="server" Text="删除" CommandArgument='<%#Eval("identifierid")%>'
                                    CommandName="Delete_F" OnClientClick="return confirm('您确认删除吗?')" />这里主要是把 ID 也就是<%#Eval("identifierid")%>给CommandArgument以便取出protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToString() == "Delete_F")
            {
                MaterialBF db = new MaterialBF();
                string identifierid = e.CommandArgument.ToString();//取出要更新信息的唯一标识            db.DeleteMyFy(identifierid);
                BindData();
                       }
        }这个在主要是用来知道哪个修改或是删除的按钮。MaterialBF db = new MaterialBF();这句是人家的自己的方法,你不用管,你只要看见string identifierid = e.CommandArgument.ToString();//取出要更新信息的唯一标识。
    这个是你要的ID值。 这里你可以采用Response.Redirect 进行跳转了。identifierid就是你的ID了。下面的代码,不用我写了吧。你试试吧。
      

  9.   

    剛剛我用的是GridView自帶的 選擇 屬性,而寫的是 編輯屬性 所以報錯~
    其實你不一定要添加按鈕,GridView有自帶的 編輯 屬性的,上面的你只要將GridView綁定的數據補上就可以用了~
      

  10.   


                        <asp:TemplateField HeaderText="编辑">
                            <ItemTemplate>
                                <asp:LinkButton ID="deleteButton" runat="server" Text="编辑" PostBackUrl='<%# Eval("ID","test.aspx?id={0}") %>' />
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Right" />
                        </asp:TemplateField>绑定PostBackUrl就行了,其中Eval("ID","test.aspx?id={0}") 的ID是数据库记录的ID,test.aspx是弹出的页面
      

  11.   

    不要到其他web里,在同一个处理方便。