我正在使用VS2005 C#。使用Dropdownlist控件更新GridView
目前,我有一個GridView,我已經改變了我的GridView控件之一,我的列名Gender
,從默認TextBox
到DropDownList
,這是我給了控制ID
到GenderList
,它包含2個值, M和F。
我有一個默認的更新語句,它能夠edit
後更新的GridView,它是這樣的:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString=
"<%$ ConnectionStrings:SODConnectionString %>" UpdateCommand="UPDATE
[UserMasterData] SET [Name] = @Name, [Age] = @Age, [ContactNo][email protected],
[Address][email protected], [Gender][email protected]"/>
以上UPDATE查詢作品完美,現在我已經改變了我Gender
文本框到一個下拉列表,在UPDATE
查詢給了我該說的錯誤:
Must declare the scalar variable "@Gender".
我假設UPDATE查詢找不到我的Gender
列中的值。
我試圖將UPDATE查詢修改爲@GenderList
,但它不起作用。
任何人都知道我應該怎麼做UPDATE查詢,以便我的UPDATE查詢可以在我的Gender
列中找到我的GenderList
下拉列表中的值?
謝謝。
下面是我以前Gender
列一個文本框控件:
<asp:BoundField HeaderText="Gender"
DataField="Gender"
SortExpression="Gender"></asp:BoundField>
下面是我Gender
與下拉列表控件:
<asp:TemplateField HeaderText="Gender" SortExpression="Gender" >
<EditItemTemplate>
<asp:DropDownList ID="GenderList" runat="server" Width="50px" >
<asp:ListItem>M</asp:ListItem>
<asp:ListItem>F</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
編輯:
嘗試過實施RowDatBound
和onRowUpdating
:
RowDatBound
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView dRowView = (DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList genderList= (DropDownList)e.Row.FindControl("GenderList");
genderList.SelectedValue = dRowView[2].ToString();
}
}
}
RowUpdating
.aspx.cs
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DropDownList genderSelect =(DropDownList)GridView1.Rows[e.RowIndex].FindControl("GenderList");
SqlDataSource1.UpdateParameters["Gender"].DefaultValue =
genderSelect.SelectedValue; --> error says not set to an instance of an object
}
http://stackoverflow.com/questions/8395365/asp-net-change-of-gridview-update-statement-after-templateediting/8395849#8395849同一個問題與一個更多新的acc ???不要用新的線索提問相同的問題,即使你是同一個用戶http://stackoverflow.com/users/872370/ruihao –
請不要創建新的賬戶,並再次提出同樣的確切問題。另外,***請停止使用「asp.net」提供您的問題。沒有必要爲標題添加標籤;我們有一個非常好的標籤系統! – Will