查询字符串在分页启用的网格视图中不起作用

时间:2012-07-31 05:50:58

标签: c# asp.net

当用户单击网格上的RegionProjectName字段时,我试图从查询字符串传递网格的RegionProjectID列。我把RegionProjectname作为超链接,下面你也可以找到代码。但这不起作用。请建议我或帮助它为什么不工作。该分页也在我的网格视图中启用。

<asp:GridView ID="ResultGridView" runat="server" AutoGenerateColumns="False" ShowFooter="true"
    DataKeyNames="RegionProjectID" 
    AllowPaging="True" 
    CellPadding="3" 
    OnPageIndexChanging="ResultGridView_PageIndexChanging" 
    OnRowDeleting="ResultGridView_RowDeleting" 
    CssClass="mGrid"
    OnRowEditing="ResultGridView_RowEditing" 
    OnRowUpdating="ResultGridView_RowUpdating" 
    OnRowCancelingEdit="ResultGridView_RowCancelingEdit" 
    PageSize="15" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellSpacing="2"
     OnRowCommand="ResultGridView_RowCommand" >
        <Columns>
            <asp:BoundField DataField="RegionProjectID" HeaderText="Region ID" InsertVisible="False"
                ReadOnly="True" SortExpression="RegionProjectID" Visible="false" />
            <asp:TemplateField HeaderText="Region Name" SortExpression="RegionProjectName">
                <EditItemTemplate>
                    <asp:TextBox ID="txtRegion" Width="250px" runat="server" Text='<%# Bind("RegionProjectName") %>'></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                <asp:TextBox ID="txtRegion1" runat="server"  Width="250px"></asp:TextBox> 
                </FooterTemplate>
                <ItemTemplate>
                   <asp:LinkButton ID="LinkButton3" CommandName="Details" CommandArgument='<%# Eval("RegionProjectID") %>' Text='<%# Bind("RegionProjectName") %>' runat="server" /> 
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Edit" ShowHeader="False"> 
            <EditItemTemplate> 
              <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton> 
              <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton> 
            </EditItemTemplate> 
            <FooterTemplate> 
              <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="AddNew" Text="Add New"></asp:LinkButton> 
            </FooterTemplate> 
            <ItemTemplate> 
              <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton> 
            </ItemTemplate> 
            </asp:TemplateField> 
            <asp:CommandField HeaderText="Delete" ShowDeleteButton="True"  ShowHeader="True" /> 
           <%-- <asp:CommandField HeaderText="Select" ShowSelectButton="True"  ShowHeader="True"/> --%>

        </Columns>
        <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
        <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
        <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
        <%--<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />--%>
        <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
    </asp:GridView>`


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

        } 

        if (e.CommandName.Equals("AddNew"))
        {

            TextBox txtRegion1 = (TextBox)ResultGridView.FooterRow.FindControl("txtRegion1");
            TextBox txtNatureOFWork1 = (TextBox)ResultGridView.FooterRow.FindControl("txtNatureOFWork1");
            if (txtRegion1.Text != "")
            {
                cmd.Connection = conn;
                cmd.CommandText = "INSERT INTO RegionAndProjectInfo(RegionProjectName, NatureOFWorkID ) Values('" + txtRegion1.Text + "', '" + ddlnatureOfWork.SelectedValue.ToString() + "')";
                conn.Open();
                cmd.ExecuteNonQuery();
            }
            FillVendorGrid();
            conn.Close();
        }
    }

2 个答案:

答案 0 :(得分:0)

您必须使用

Response.Redirect("Default3.aspx?ID=" + e.CommandArgument.ToString());

而不是服务器传输,导致服务器传输与Response.Redirect相比做了不同的事情。

Server.Transfer方法还有第二个参数 - “preserveForm”。如果将此值设置为True,则使用

等语句
`Server.Transfer("Default3.aspx?ID=" + e.CommandArgument.ToString()`, True);

,现有的查询字符串和任何表单变量仍可供您传输的页面使用。

更多的是文件传输,而不是重定向

http://msdn.microsoft.com/en-us/library/ms525800%28v=vs.90%29.aspx

答案 1 :(得分:0)

<ItemTemplate>
                   <asp:hyperlink id="LinkButton3" navigateurl='<%# "Default3.aspx?ID=" + Eval("RegionProjectID")%>' text='<%# Bind("RegionProjectName")%>' runat="server" />  
                </ItemTemplate>

我已经更新了Regionprojectname的itemtemplate,现在获取了查询字符串。