从下拉菜单中获取值,运行查询并向formviewc#添加值
本文关键字:formviewc# 添加 查询 下拉菜单 获取 运行 | 更新日期: 2023-09-27 18:14:20
我有一个asp.net webform,其中包含一个formView, 3个下拉菜单和一个提交按钮。下拉菜单的值来自数据库。
当用户单击提交按钮时,下拉菜单中的值应该通过我们的查询运行,并在formView中显示结果。这是不可能的。
当我们在callSelectProduct()中给出其他、肉类和蔬菜的标准值时,我们可以在表单视图中看到正确的输出,但这是在页面加载时。
这是来自提交按钮的click方法:
protected void getRecipe(object sender, EventArgs e)
{
string ddlOther = DropDownOther.SelectedValue;
string ddlVegetables = DropDownVegetables.SelectedValue;
string ddlMeat = DropDownMeat.SelectedValue;
int ddlIntOther = int.Parse(ddlOther);
int ddlIntVegetables = int.Parse(ddlVegetables);
int ddlIntMeat = int.Parse(ddlMeat);
Business.Class1.callSelectProduct(ddlIntOther, ddlIntMeat, ddlIntVegetables);
}
这是callSelectProduct():调试。WriteLine将正确的值返回到调试控制台中,但随后由于单击提交按钮和调试按钮,页面重新加载。WriteLine返回0 0 0。我想这就是为什么我在FormView中看不到任何东西。因为0 0 0的组合不会返回任何东西
public static void callSelectProduct(int other, int meat, int vegetables)
{
SelectProduct(other, meat, vegetables);
}
[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)]
public static Data.SouthWind.SelectRecipesFromIngredientsDataTable SelectProduct(int otherGet, int meatGet, int vegetablesGet)
{
int other = otherGet;
int meat = meatGet;
int vegetables = vegetablesGet;
System.Diagnostics.Debug.WriteLine("This is class 1 Other " + other + " Vegetable " + vegetables + " Meat " + meat);
DataAccess.SouthWindTableAdapters.SelectRecipesFromIngredientsTableAdapter tableAdaptertest = new DataAccess.SouthWindTableAdapters.SelectRecipesFromIngredientsTableAdapter();
return tableAdaptertest.GetData(other, meat, vegetables);
}
这是我们的webform:
<form id="form1" runat="server">
<div>
<asp:FormView ID="RecipeFormView" runat="server" AllowPaging="True" DataSourceID="ObjectDataSource1">
<EditItemTemplate>
RecipeName:
<asp:TextBox ID="RecipeNameTextBox" runat="server" Text='<%# Bind("RecipeName") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
RecipeName:
<asp:TextBox ID="RecipeNameTextBox" runat="server" Text='<%# Bind("RecipeName") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
RecipeName:
<asp:Label ID="RecipeNameLabel" runat="server" Text='<%# Bind("RecipeName") %>' />
<br />
</ItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="SelectProduct" TypeName="Business.Class1">
<SelectParameters>
<asp:Parameter Name="otherGet" Type="Int32" />
<asp:Parameter Name="meatGet" Type="Int32" />
<asp:Parameter Name="vegetablesGet" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:DropDownList ID="DropDownOther" runat="server" DataSourceID="ObjectDataSource2" DataTextField="IngredientName" DataValueField="IngredientId">
</asp:DropDownList>
<asp:DropDownList ID="DropDownVegetables" runat="server" DataSourceID="SelectVegetables" DataTextField="IngredientName" DataValueField="IngredientId" Height="16px">
</asp:DropDownList>
<asp:DropDownList ID="DropDownMeat" runat="server" DataSourceID="SelectMeat" DataTextField="IngredientName" DataValueField="IngredientId">
</asp:DropDownList>
<asp:ObjectDataSource ID="SelectMeat" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="SelectMeat" TypeName="Business.Class1"></asp:ObjectDataSource>
<asp:ObjectDataSource ID="SelectVegetables" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="SelectVegetables" TypeName="Business.Class1"></asp:ObjectDataSource>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="SelectOther" TypeName="Business.Class1"></asp:ObjectDataSource>
<asp:Button ID="Button1" runat="server" OnClick="getRecipe" Text="Button" UseSubmitBehavior="False" />
<br />
</div>
</form>
欢迎任何帮助!
你是否使用了!IsPostBack在外部的下拉绑定方法在页面加载事件的内部?
void page-load()
{
if(!IsPostBack)
{
//Call all dropdownlist binding method
}
}