0

フッターテンプレートに新しい顧客を挿入できる顧客Gridviewがあります。

挿入ボタンを押すと、同じページにリダイレクトされ、グリッドビューに新しい顧客が表示されます。

ページを直接更新すると、同じ顧客が再び追加されることに気付きました。リダイレクト後、名、姓などの必須フィールドはもちろんすべて空ですが、ページは以前の値を取得して別の挿入を行うため、これは私にとって本当に奇妙です。

なぜそうなのか、どうすればこの問題を解決できますか?

aspx

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" CellPadding="3" DataKeyNames="CustID" DataSourceID="Event" GridLines="Horizontal" ShowFooter="True" AllowPaging="True" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" OnRowDataBound="GridView1_RowDataBound">
    <AlternatingRowStyle BackColor="#F7F7F7" HorizontalAlign="Center" />
    <Columns>
        <asp:CommandField ShowEditButton="True" />
        <asp:TemplateField HeaderText="ID" InsertVisible="False" SortExpression="CustID">
            <EditItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("CustID") %>'></asp:Label>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("CustID") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:LinkButton ValidationGroup="INSERT" OnClick="lbInsert_Click"  ID="lbInsert" runat="server" >Insert</asp:LinkButton>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Anrede" SortExpression="Gender">
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource1" DataTextField="Gender" DataValueField="Gender" SelectedValue='<%# Bind("Gender") %>'>
                </asp:DropDownList>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:EventConnectionString %>" SelectCommand="SELECT DISTINCT [Gender] FROM [Customer]"></asp:SqlDataSource>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Gender") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <div style="text-align: center;"><asp:Label ID="Label13" runat="server" Text="Anrede"></asp:Label>
                <asp:DropDownList ID="ddlGender" runat="server">
                    <asp:ListItem>Anrede Auswählen</asp:ListItem>
                    <asp:ListItem>Herr</asp:ListItem>
                    <asp:ListItem>Frau</asp:ListItem>
                </asp:DropDownList>
                <asp:RequiredFieldValidator ID="rfvInsGender" runat="server" ErrorMessage="Anrede ist ein Pflichtfeld"
                    ControlToValidate="ddlGender" ValidationGroup="INSERT" Text="*" ForeColor="Red" InitialValue="Anrede Auswählen">
                </asp:RequiredFieldValidator>
                </div>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name" SortExpression="LastName">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvEdLastName" runat="server" ErrorMessage="Name ist ein Pflichtfeld"
                    ControlToValidate="TextBox2" Text="*" ForeColor="Red">
                </asp:RequiredFieldValidator>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <div style="text-align: center;"><asp:Label ID="Label14" runat="server" Text="Name"></asp:Label></div>
                <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvInsLastName" runat="server" ErrorMessage="Name ist ein Pflichtfeld"
                    ControlToValidate="txtLastName" ValidationGroup="INSERT" Text="*" ForeColor="Red">
                </asp:RequiredFieldValidator>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Vorname" SortExpression="FirstName">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvEdFirstName" runat="server" ErrorMessage="VorName ist ein Pflichtfeld"
                    ControlToValidate="TextBox3" Text="*" ForeColor="Red">
                </asp:RequiredFieldValidator>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label4" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <div style="text-align: center;"><asp:Label ID="Label15" runat="server" Text="Vorname"></asp:Label></div>
                <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvInsFirstName" runat="server" ErrorMessage="VorName ist ein Pflichtfeld"
                    ControlToValidate="txtFirstName" ValidationGroup="INSERT" Text="*" ForeColor="Red">
                </asp:RequiredFieldValidator>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Strasse" SortExpression="Street">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Street") %>'></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvEdStreet" runat="server" ErrorMessage="Strasse ist ein Pflichtfeld"
                    ControlToValidate="TextBox4" Text="*" ForeColor="Red">
                </asp:RequiredFieldValidator>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label5" runat="server" Text='<%# Bind("Street") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <div style="text-align: center;"><asp:Label ID="Label16" runat="server" Text="Strasse"></asp:Label></div>
                <asp:TextBox ID="txtStreet" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvInsStreet" runat="server" ErrorMessage="Strasse ist ein Pflichtfeld"
                    ControlToValidate="txtStreet" ValidationGroup="INSERT" Text="*" ForeColor="Red">
                </asp:RequiredFieldValidator>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="House Nr" SortExpression="HouseNr">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("HouseNr") %>'></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvEdHouseNr" runat="server" ErrorMessage="House Nr ist ein Pflichtfeld"
                    ControlToValidate="TextBox5" Text="*" ForeColor="Red">
                </asp:RequiredFieldValidator>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label6" runat="server" Text='<%# Bind("HouseNr") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <div style="text-align: center;"><asp:Label ID="Label17" runat="server" Text="House Nr"></asp:Label></div>
                <asp:TextBox ID="txtHouseNr" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvInsHouseNr" runat="server" ErrorMessage="House Nr ist ein Pflichtfeld"
                    ControlToValidate="txtHouseNr" ValidationGroup="INSERT" Text="*" ForeColor="Red">
                </asp:RequiredFieldValidator>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="PLZ" SortExpression="Zip">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("Zip") %>'></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvEdZip" runat="server" ErrorMessage="PLZ ist ein Pflichtfeld"
                    ControlToValidate="TextBox6" Text="*" ForeColor="Red">
                </asp:RequiredFieldValidator>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label7" runat="server" Text='<%# Bind("Zip") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <div style="text-align: center;"><asp:Label ID="Label18" runat="server" Text="PLZ"></asp:Label></div>
                <asp:TextBox ID="txtZip" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvInsZip" runat="server" ErrorMessage="PLZ ist ein Pflichtfeld"
                    ControlToValidate="txtZip" ValidationGroup="INSERT" Text="*" ForeColor="Red">
                </asp:RequiredFieldValidator>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Ort" SortExpression="City">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("City") %>'></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvEdCity" runat="server" ErrorMessage="Ort ist ein Pflichtfeld"
                    ControlToValidate="TextBox7" Text="*" ForeColor="Red">
                </asp:RequiredFieldValidator>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label8" runat="server" Text='<%# Bind("City") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <div style="text-align: center;"><asp:Label ID="Label19" runat="server" Text="Ort"></asp:Label></div>
                <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvInsCity" runat="server" ErrorMessage="Ort ist ein Pflichtfeld"
                    ControlToValidate="txtCity" ValidationGroup="INSERT" Text="*" ForeColor="Red">
                </asp:RequiredFieldValidator>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Telefon" SortExpression="Phone">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("Phone") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label9" runat="server" Text='<%# Bind("Phone") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <div style="text-align: center;"><asp:Label ID="Label20" runat="server" Text="Telefon"></asp:Label></div>
                <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="E-mail" SortExpression="Email">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("Email") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label10" runat="server" Text='<%# Bind("Email") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <div style="text-align: center;"><asp:Label ID="Label21" runat="server" Text="E-mail"></asp:Label></div>
                <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Firma" SortExpression="Company">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox10" runat="server" Text='<%# Bind("Company") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label11" runat="server" Text='<%# Bind("Company") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <div style="text-align: center;"><asp:Label ID="Label22" runat="server" Text="Firma"></asp:Label></div>
                <asp:TextBox ID="txtCompany" runat="server"></asp:TextBox>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Status" SortExpression="Active">
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList4" runat="server" DataSourceID="SqlDataSource2" DataTextField="Active" DataValueField="Active" SelectedValue='<%# Bind("Active") %>'>
                </asp:DropDownList>
                <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:EventConnectionString %>" SelectCommand="SELECT DISTINCT [Active] FROM [Customer]"></asp:SqlDataSource>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label12" runat="server" Text='<%# Bind("Active") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <div style="text-align: center;"><asp:Label ID="Label23" runat="server" Text="Status"></asp:Label></div>
                <asp:DropDownList ID="ddlActive" runat="server">
                    <asp:ListItem>Status Auswählen</asp:ListItem>
                    <asp:ListItem>Aktiv</asp:ListItem>
                    <asp:ListItem>Passiv</asp:ListItem>
                </asp:DropDownList>
                <asp:RequiredFieldValidator ID="rfvInsActive" runat="server" ErrorMessage="Status ist ein Pflichtfeld"
                    ControlToValidate="ddlActive" ValidationGroup="INSERT" Text="*" ForeColor="Red" InitialValue="Status Auswählen">
                </asp:RequiredFieldValidator>
            </FooterTemplate>
        </asp:TemplateField>

    </Columns>
    <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
    <HeaderStyle BackColor="#B5C7DE" Font-Bold="True" ForeColor="#4A3C8C" />
    <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
    <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
    <SortedAscendingCellStyle BackColor="#F4F4FD" />
    <SortedAscendingHeaderStyle BackColor="#5A4C9D" />
    <SortedDescendingCellStyle BackColor="#D8D8F0" />
    <SortedDescendingHeaderStyle BackColor="#3E3277" />
</asp:GridView>
</div>
<asp:ValidationSummary ValidationGroup="INSERT" ID="ValidationSummary1" ForeColor="Red" runat="server" />
<asp:ValidationSummary ID="ValidationSummary2" ForeColor="Red" runat="server" />
<asp:SqlDataSource ID="Event" runat="server" ConnectionString="<%$ ConnectionStrings:EventConnectionString %>" DeleteCommand="DELETE FROM [Customer] WHERE [CustID] = @CustID" InsertCommand="INSERT INTO [Customer] ([Gender], [LastName], [FirstName], [Street], [HouseNr], [Zip], [City], [Phone], [Email], [Company], [Active]) VALUES (@Gender, @LastName, @FirstName, @Street, @HouseNr, @Zip, @City, @Phone, @Email, @Company, @Active)" SelectCommand="SELECT [CustID], [Gender], [LastName], [FirstName], [Street], [HouseNr], [Zip], [City], [Phone], [Email], [Company], [Active] FROM [Customer]" UpdateCommand="UPDATE [Customer] SET [Gender] = @Gender, [LastName] = @LastName, [FirstName] = @FirstName, [Street] = @Street, [HouseNr] = @HouseNr, [Zip] = @Zip, [City] = @City, [Phone] = @Phone, [Email] = @Email, [Company] = @Company, [Active] = @Active WHERE [CustID] = @CustID">
    <DeleteParameters>
        <asp:Parameter Name="CustID" Type="Decimal" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="Gender" Type="String" />
        <asp:Parameter Name="LastName" Type="String" />
        <asp:Parameter Name="FirstName" Type="String" />
        <asp:Parameter Name="Street" Type="String" />
        <asp:Parameter Name="HouseNr" Type="String" />
        <asp:Parameter Name="Zip" Type="String" />
        <asp:Parameter Name="City" Type="String" />
        <asp:Parameter Name="Phone" Type="String" />
        <asp:Parameter Name="Email" Type="String" />
        <asp:Parameter Name="Company" Type="String" />
        <asp:Parameter Name="Active" Type="String" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="Gender" Type="String" />
        <asp:Parameter Name="LastName" Type="String" />
        <asp:Parameter Name="FirstName" Type="String" />
        <asp:Parameter Name="Street" Type="String" />
        <asp:Parameter Name="HouseNr" Type="String" />
        <asp:Parameter Name="Zip" Type="String" />
        <asp:Parameter Name="City" Type="String" />
        <asp:Parameter Name="Phone" Type="String" />
        <asp:Parameter Name="Email" Type="String" />
        <asp:Parameter Name="Company" Type="String" />
        <asp:Parameter Name="Active" Type="String" />
        <asp:Parameter Name="CustID" Type="Decimal" />
    </UpdateParameters>
</asp:SqlDataSource>

aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void lbInsert_Click(object sender, EventArgs e)
    {
        Event.InsertParameters["Gender"].DefaultValue = ((DropDownList)GridView1.FooterRow.FindControl("ddlGender")).SelectedValue;
        Event.InsertParameters["LastName"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtLastName")).Text;
        Event.InsertParameters["FirstName"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtFirstName")).Text;
        Event.InsertParameters["Street"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtStreet")).Text;
        Event.InsertParameters["HouseNr"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtHouseNr")).Text;
        Event.InsertParameters["Zip"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtZip")).Text;
        Event.InsertParameters["City"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtCity")).Text;
        Event.InsertParameters["Phone"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtPhone")).Text;
        Event.InsertParameters["Email"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtEmail")).Text;
        Event.InsertParameters["Company"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtCompany")).Text;
        Event.InsertParameters["Active"].DefaultValue = ((DropDownList)GridView1.FooterRow.FindControl("ddlActive")).SelectedValue;

        Event.Insert();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var dr = e.Row.DataItem as DataRowView;

            if (dr["CustID"].ToString() == "60" || dr["CustID"].ToString() == "62")
            {
                e.Row.Enabled = false;  //OR dr.Enabled = false;
                //DISABLED Controls only     
                //((TextBox)e.Row.FindControl("TextBox1")).Enabled = false;
            }
        }
    }
4

3 に答える 3

0

あなたがすべて必要とするのは、使用することです

Response.Redirect(Request.Url.AbsolutePath);

ボタンクリックイベントコードの下部で使用します

 protected void lbInsert_Click(object sender, EventArgs e)
    {
        Event.InsertParameters["Gender"].DefaultValue = ((DropDownList)GridView1.FooterRow.FindControl("ddlGender")).SelectedValue;
        Event.InsertParameters["LastName"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtLastName")).Text;
        Event.InsertParameters["FirstName"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtFirstName")).Text;
        Event.InsertParameters["Street"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtStreet")).Text;
        Event.InsertParameters["HouseNr"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtHouseNr")).Text;
        Event.InsertParameters["Zip"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtZip")).Text;
        Event.InsertParameters["City"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtCity")).Text;
        Event.InsertParameters["Phone"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtPhone")).Text;
        Event.InsertParameters["Email"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtEmail")).Text;
        Event.InsertParameters["Company"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtCompany")).Text;
        Event.InsertParameters["Active"].DefaultValue = ((DropDownList)GridView1.FooterRow.FindControl("ddlActive")).SelectedValue;

        Event.Insert();
        Response.Redirect(Request.Url.AbsolutePath);
    }
于 2013-11-06T09:30:53.560 に答える
0

グリッドをバインドするメソッドを 1 つ作成し、データをグリッドに挿入するときに、このメソッドを呼び出してグリッドをバインドするだけではありません...これを試してください...

于 2013-08-23T08:59:10.613 に答える
0
private void BindGrid()
{
    //Your Code For Fill the Grid...Possibly If You Use Database than Fired Query to fetch data and fill Grid...
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var dr = e.Row.DataItem as DataRowView;

        if (dr["CustID"].ToString() == "60" || dr["CustID"].ToString() == "62")
        {
            e.Row.Enabled = false;  //OR dr.Enabled = false;
            //DISABLED Controls only     
            //((TextBox)e.Row.FindControl("TextBox1")).Enabled = false;
        }
    }
    BindGrid();
}
于 2013-08-23T09:49:23.523 に答える