问题描述
我有一个的ListView
与的DropDownList
里面。我都试过:填写的DropDownList
在 OnItemDataBound
和的Page_Load
。我也试过检查回传
,但没有什么帮助。
当我试图让价值,我总是第一个。据我所知,这是因为列表得到重新填充,但如何避免呢?如果我填补他们只有当的IsPostBack = FALSE
,他们成了空。
这是我的code:
< ASP:ListView控件=服务器ID =MyListView>
<&LayoutTemplate模板GT;
<表格边框=0的cellpadding =2CELLSPACING =0>
&所述; TR>
百分位>< ASP:文字=服务器文本='<%$资源:资源,LabelWhole%GT;' />< /第i
百分位>< ASP:文字=服务器文本='<%$资源:资源,LabelImport%GT;' />< /第i
百分位>< ASP:文字=服务器文本='<%$资源:资源,LabelPeriod%GT;' />< /第i
百分位>< ASP:文字=服务器文本='<%$资源:资源,LabelDate%GT;' />< /第i
百分位>< ASP:文字=服务器文本='<%$资源:资源,LabelUpload%GT;' />< /第i
< / TR>
< ASP:占位符ID =itemPlaceholder=服务器/>
< /表>
< / LayoutTemplate模板>
<&ItemTemplate中GT;
&所述; TR>
< TD><%#DataBinder.Eval的(的Container.DataItem,整体)%GT;< / TD>
< TD><%#DataBinder.Eval的(的Container.DataItem,上传)%>< / TD>
< TD><%#DataBinder.Eval的(的Container.DataItem,周期)%GT;< / TD>
< TD>< ASP:DropDownList的=服务器ID =DaysDropDownList>< / ASP:DropDownList的>< / TD>
< TD>< ASP:文件上传ID =文件上传=服务器/>< / TD>
< / TR>
< / ItemTemplate中>
< / ASP:的ListView>的foreach(在MyListView.Items VAR LVITEM)
{
如果(lvItem.ItemType == ListViewItemType.DataItem)
{
DropDownList的博士= lvItem.FindControl(DaysDropDownList)作为DropDownList的;
的for(int i = -1; I> = -12;我 - )
{
dr.Items.Add(新的ListItem(
DateTime.Now.AddMonths(ⅰ)的ToString(YYYY MM),
DateTime.Now.AddMonths(ⅰ)的ToString(YYYY MM)));
}
}
}
codebehind:
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
this.Application.DataLayer.LoadData(用户名);
this.MyListView.DataSource = this.Application.DataLayer.Data;
this.MyListView.DataBind(); LoadDropDownLists();
}私人无效LoadDropDownLists()
{
的foreach(在MyListView.Items VAR LVITEM)
{
如果(lvItem.ItemType == ListViewItemType.DataItem)
{
DropDownList的博士= lvItem.FindControl(DaysDropDownList)作为DropDownList的;
dr.Items.Clear();
的for(int i = -1; I> = -12;我 - )
{
dr.Items.Add(新的ListItem(
DateTime.Now.AddMonths(ⅰ)的ToString(YYYY MM),
DateTime.Now.AddMonths(ⅰ)的ToString(YYYY MM)));
}
}
}
}公共无效SubmitButtonClick(对象发件人,EventArgs的发送)
{
的foreach(在MyListView.Items VAR LVITEM)
{
如果(lvItem.ItemType == ListViewItemType.DataItem)
{
DropDownList的博士= lvItem.FindControl(DaysDropDownList)作为DropDownList的;
文件上传FL = lvItem.FindControl(文件上传)的文件上传;
如果(fl.HasFile)
{
字符串文件名= UploadFile(FL,dr.SelectedValue);
this.Application.DataLayer.SaveUploadRecord(用户ID,0,(INT)UploadType.Upload,dr.SelectedValue,文件名);
}
}
}
}
创建您绑定DropDownList的一个函数,调用此函数每次页面加载时(在Page_Load事件),但调用之前,先清除现有的DropDownList的数据源。
I have a ListView
with DropDownList
inside. I tried both: fill DropDownList
on OnItemDataBound
and in Page_Load
. I also tried checking PostBack
, but nothing helps.
When I try to get value, I always get the first one. I understand that it is because lists get refilled, but how to avoid it? If I fill them only when IsPostBack = false
, they became empty.
This is my code:
<asp:ListView runat="server" ID="MyListView">
<LayoutTemplate>
<table border="0" cellpadding="2" cellspacing="0">
<tr>
<th><asp:Literal runat="server" Text='<%$ Resources: Resource, LabelWhole %>' /></th>
<th><asp:Literal runat="server" Text='<%$ Resources: Resource, LabelImport %>' /></th>
<th><asp:Literal runat="server" Text='<%$ Resources: Resource, LabelPeriod %>' /></th>
<th><asp:Literal runat="server" Text='<%$ Resources: Resource, LabelDate %>' /></th>
<th><asp:Literal runat="server" Text='<%$ Resources: Resource, LabelUpload %>' /></th>
</tr>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem, "Whole") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "Upload")%></td>
<td><%# DataBinder.Eval(Container.DataItem, "Period")%></td>
<td><asp:DropDownList runat="server" ID="DaysDropDownList"></asp:DropDownList></td>
<td><asp:FileUpload ID="FileUpload" runat="server" /></td>
</tr>
</ItemTemplate>
</asp:ListView>
foreach (var lvItem in MyListView.Items)
{
if (lvItem.ItemType == ListViewItemType.DataItem)
{
DropDownList dr = lvItem.FindControl("DaysDropDownList") as DropDownList;
for (int i = -1; i >= -12; i--)
{
dr.Items.Add(new ListItem(
DateTime.Now.AddMonths(i).ToString("yyyy MM"),
DateTime.Now.AddMonths(i).ToString("yyyy MM")));
}
}
}
Codebehind:
protected void Page_Load(object sender, EventArgs e)
{
this.Application.DataLayer.LoadData(UserID);
this.MyListView.DataSource = this.Application.DataLayer.Data;
this.MyListView.DataBind();
LoadDropDownLists();
}
private void LoadDropDownLists()
{
foreach (var lvItem in MyListView.Items)
{
if (lvItem.ItemType == ListViewItemType.DataItem)
{
DropDownList dr = lvItem.FindControl("DaysDropDownList") as DropDownList;
dr.Items.Clear();
for (int i = -1; i >= -12; i--)
{
dr.Items.Add(new ListItem(
DateTime.Now.AddMonths(i).ToString("yyyy MM"),
DateTime.Now.AddMonths(i).ToString("yyyy MM")));
}
}
}
}
public void SubmitButtonClick(object sender, EventArgs e)
{
foreach (var lvItem in MyListView.Items)
{
if (lvItem.ItemType == ListViewItemType.DataItem)
{
DropDownList dr = lvItem.FindControl("DaysDropDownList") as DropDownList;
FileUpload fl = lvItem.FindControl("FileUpload") as FileUpload;
if (fl.HasFile)
{
string filename = UploadFile(fl, dr.SelectedValue);
this.Application.DataLayer.SaveUploadRecord(UserID, 0, (int)UploadType.Upload, dr.SelectedValue, filename);
}
}
}
}
Create a function in which you bind the dropdownlist, Call this function everytime when the page loads(in the page_load event), but before calling it first clear the existing dropdownlist datasource.
这篇关于DropDownList的ListView中了selectedValue永远是第一位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!