在ListView中使用DropDownList更新 [英] Update Using DropDownList In ListView
问题描述
我在ListView的编辑"模式下使用DropDownList.所有其他列均已更新,但下拉列表中的所选项目未更新,尽管我强制将下拉列表的新值传递给存储过程,但列表视图仍将旧值传递给存储过程.那么如何将新值传递给数据库.
这是我的列表视图
项目模板的图像在这里
http://www.freeimagehosting.net/bc3hm
这是列表视图的ItemTemplate
Hi,
I am using DropDownList in Edit Mode of ListView. All other columns are updated, but dropdowns selected item is not updating, listview is passing old values to store procedure although i am forcefully passing new values of dropdown to store procedure. So how to pass new values to database.
This is my List View
Image of Item Template is here
http://www.freeimagehosting.net/bc3hm
This is ItemTemplate of List View
<ItemTemplate>
<tr >
<td>
<asp:Label ID="CountryCodeLabel" runat="server" Text='<%# Eval("CountryCode") %>'></asp:Label>
</td>
<td >
<asp:Label ID="CountryNameLabel" runat="server" Text='<%# Eval("CountryName") %>'></asp:Label>
</td>
<td>
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</td>
<td>
<asp:ImageButton runat=server ID=NameEdit ImageUrl="~/Image/edit.png" Width=20px CommandName="Edit" />   <asp:ImageButton runat=server ID="NameDelete" CommandName="Delete" ImageUrl="~/Image/delete.png" Width=20px />
</td>
</tr>
</ItemTemplate>
这是编辑模式
用于编辑的图像在这里
http://www.freeimagehosting.net/8cxrt</a>
我的编辑模板是
This is Edit Mode
Image For Edit is Here
http://www.freeimagehosting.net/8cxrt</a>
And my Edit Template is
<EditItemTemplate>
<tr>
<td>
<asp:TextBox ID="CodeTextBox" runat="server" Text='<%# Bind("CountryCode") %>' ></asp:TextBox>
</td>
<td >
<asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("CountryName") %>' ></asp:TextBox>
</td>
<td>
<asp:DropDownList ID="NameDropDownList" runat="server" DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="NameId" >
</asp:DropDownList>
</td>
<td>
<asp:ImageButton runat=server ID=UpdateSave ImageUrl="~/Image/save.png" Width=20px CommandName="Update" />   <asp:ImageButton runat=server ID="UpdateCancel" CommandName="Cancel" ImageUrl="~/Image/undo.png" Width=20px />
</td>
</tr>
</EditItemTemplate>
对于更新,我正在处理此事件
and For Updating i am handling this event
protected void ListView_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
DropDownList NameDropDownList = (DropDownList)((ListView)sender).Items[e.ItemIndex].FindControl("NameDropDownList ");
if (NameDropDownList!= null)
{
SqlDataSource1.UpdateParameters["NameId"].DefaultValue = NameDropDownList.SelectedValue;
}
}
所以请帮我解决这个问题.
在此先感谢.
So please help me how to solve that problem.
Thanks In Advance.
推荐答案
我的事情是,当您单击更新按钮时,页面会刷新,那时下拉列表的值正在更改
尝试这样
受保护的void ListView_ItemUpdating(对象发送者,ListViewUpdateEventArgs e)
{
DropDownList NameDropDownList =(DropDownList)((ListView)sender).Items [e.ItemIndex] .FindControl("NameDropDownList");
如果(NameDropDownList!= null)
{
if(!IsPostBack)
{
SqlDataSource1.UpdateParameters ["NameId"].DefaultValue = NameDropDownList.SelectedValue;
}
}
}
i thing when ever your clicking update button the page is refreshing at that time the dropdownlist values are changing
try like this
protected void ListView_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
DropDownList NameDropDownList = (DropDownList)((ListView)sender).Items[e.ItemIndex].FindControl("NameDropDownList ");
if (NameDropDownList!= null)
{
if(!IsPostBack )
{
SqlDataSource1.UpdateParameters["NameId"].DefaultValue = NameDropDownList.SelectedValue;
}
}
}
这篇关于在ListView中使用DropDownList更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!