代码如下
<asp:ListBox ID="lbTank" runat="server" Width="120px" Height="60px" DataSourceID="ODSTank" DataTextField="Key" DataValueField="Value" SelectionMode="Multiple"></asp:ListBox> ListBox lbTank = ManageBoxFormView.FindControl("lbTank") as ListBox; //倒出罐
string tempstr = "";
for (int i = 0; i < lbTank.Items.Count; i++)
{
if (lbTank.Items[i].Selected) //这儿始终只能读取到选中的第一个值,不知为什么
{
if (string.IsNullOrEmpty(tempstr))
{
tempstr = lbTank.Items[i].Value;
}
else
{
tempstr = tempstr + "," + lbTank.Items[i].Value;
}
}
}
<asp:ListBox ID="lbTank" runat="server" Width="120px" Height="60px" DataSourceID="ODSTank" DataTextField="Key" DataValueField="Value" SelectionMode="Multiple"></asp:ListBox> ListBox lbTank = ManageBoxFormView.FindControl("lbTank") as ListBox; //倒出罐
string tempstr = "";
for (int i = 0; i < lbTank.Items.Count; i++)
{
if (lbTank.Items[i].Selected) //这儿始终只能读取到选中的第一个值,不知为什么
{
if (string.IsNullOrEmpty(tempstr))
{
tempstr = lbTank.Items[i].Value;
}
else
{
tempstr = tempstr + "," + lbTank.Items[i].Value;
}
}
}
可以试试直接使用lbTank.Items[i].Selected,不倒出罐验证一下。
需要更改一下
也许更改一下默认设置就行了。
try it
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
<asp:ListItem Text="3" Value="3"></asp:ListItem>
<asp:ListItem Text="4" Value="4"></asp:ListItem>
</asp:ListBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
protected void Button1_Click(object sender, EventArgs e)
{
string item = "";
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected)
{
if (string.IsNullOrEmpty(item))
{
item = ListBox1.Items[i].Value;
}
else
{
item += "," + ListBox1.Items[i].Value;
}
}
}
Response.Write(item);
}没有问题啊,LZ试试
ListBox lbTank = ManageBoxFormView.FindControl("lbTank") as ListBox; string[] tempstr3 = Request.Form.GetValues(lbTank.UniqueID); //当我在里面选中两个就显示两个一样数据数组,而且始终是第一个,如果选中三个,可能是显示3个大小的第一个选中数据的数组
<asp:UpdatePanel ID="ManageBoxUpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div class="riPanel_content_div">
<div style="height: 7px;">
</div>
<dxrp:ASPxRoundPanel ID="RoundPanelForNew" runat="server" Width="100%" HeaderText="添加业务通知单">
<PanelCollection>
<dxrp:PanelContent>
<asp:FormView ID="ManageBoxFormView" runat="server" OnItemInserted="ManageBoxFormView_ItemInserted"
OnItemUpdated="ManageBoxFormView_ItemUpdated" DataSourceID="ManageBoxDataSource"
OnDataBound="ManageBoxFormView_DataBound" DefaultMode="Insert"
OnItemInserting="ManageBoxFormView_ItemInserting">
<EditItemTemplate> //这个我就不贴了 </EditItemTemplate>
<InsertItemTemplate>
<div class="FormViewList">
<%--编号--%>
<asp:Label ID="Label4" runat="server" Text="编号:" Width="60px" AssociatedControlID="LabNoticeID"></asp:Label>
<asp:Label ID="LabNoticeID" runat="server" Text='<%# Bind("NoticeID") %>' CssClass="labelLeft"
AssociatedControlID="Label4" Width="120px" />
</div>
<div class="FormViewList">
<asp:Label ID="Label1" runat="server" Text="类型:" Width="60px" AssociatedControlID="ddlNoticeType"></asp:Label>
<asp:DropDownList ID="ddlNoticeType" runat="server" Width="128px" SelectedValue='<%# Bind("NoticeType") %>'
AutoPostBack="true" OnSelectedIndexChanged="NoticeType_SelectedIndexChanged">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="ddlNoticeType"
ErrorMessage="类型必须选择" ValidationGroup="VGroup" Display="Dynamic">*</asp:RequiredFieldValidator>
<cc1:CascadingDropDown ID="ajaxcddNoticeType" runat="server" TargetControlID="ddlNoticeType"
Category="noticetype" PromptText="选择类型" LoadingText="[Loading...]" ServicePath="StorageAutoComplete.asmx"
ServiceMethod="GetNoticeTypeForWareHouse" />
<asp:Label ID="Label7" runat="server" Text="液化品:" Width="60px" AssociatedControlID="ddlOilInfo"></asp:Label>
<asp:DropDownList ID="ddlOilInfo" runat="server" Width="128px"
SelectedValue='<%# Bind("NoticeOilID") %>'
onselectedindexchanged="ddlOilInfo_TextChanged" AutoPostBack="true">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="ddlOilInfo"
ErrorMessage="液化品必须选择" ValidationGroup="VGroup" Display="Dynamic">*</asp:RequiredFieldValidator>
</div>
<div class="FormViewList">
<asp:Label ID="Label17" runat="server" Text="储罐:" Width="60px" AssociatedControlID="lbTank"></asp:Label>
<asp:ListBox ID="lbTank" runat="server" Width="120px" Height="60px"
DataSourceID="ODSTank" DataTextField="Key" DataValueField="Value"
SelectionMode="Multiple" ></asp:ListBox>
<asp:HiddenField ID="HDlbTank" runat="server" Value='<%# Bind("NoticeTool") %>' />
<asp:Label ID="lblTankIDTit" runat="server" AssociatedControlID="lbInTank"
Text="倒入罐:" Width="60px"></asp:Label>
<asp:ListBox ID="lbInTank" runat="server" Width="120px" Height="60px"
DataSourceID="ODSTank" DataTextField="Key" DataValueField="Value"
SelectionMode="Multiple" ></asp:ListBox>
<asp:HiddenField ID="HDlbInTank" runat="server" Value='<%# Bind("TankID") %>'/>
</div>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" EnableClientScript="true"
ValidationGroup="VGroup" />
</div>
<div class="FormViewButton">
<table>
<tr>
<td>
<dxe:ASPxButton ID="InsertButton" runat="server" Text="添 加" ValidationGroup="VGroup"
CausesValidation="True" AutoPostBack="false">
<Image Url="~/App_Themes/Default/Icon/BtnAdd.png" UrlDisabled="~/App_Themes/Default/Icon/BtnAdd_Disable.png" />
<ClientSideEvents Click="
function(s, e) {
if (Page_ClientValidate('VGroup')){
aa();
var lbTank=$get('ctl00_MainContent_RoundPanelForNew_ManageBoxFormView_lbTank')
for(i=0;i<lbTank.length;i++)
{
if(lbTank.options[i].selected)
{
alert(lbTank.options[i].text)
}
}
$find('ManageBoxBehavior').hide();
mainGrid.PerformCallback('InsertItem');
}
}" />
</dxe:ASPxButton>
</td>
<td>
<dxe:ASPxButton ID="UpdateCancelButton" runat="server" Text="取 消" CausesValidation="False"
CommandName="Cancel">
</dxe:ASPxButton>
</td>
</tr>
</table>
</div>
</InsertItemTemplate>
</asp:FormView>
</dxrp:PanelContent>
</PanelCollection>
</dxrp:ASPxRoundPanel>
</div>
<asp:ObjectDataSource ID="ManageBoxDataSource" runat="server" DataObjectTypeName="PPMIS.DAL.NoticeInfo"
InsertMethod="CreateWareHouseNotice" SelectMethod="ShowNoticeDetail" UpdateMethod="UpdateWareHouseNotice"
OldValuesParameterFormatString="original_{0}" TypeName="PPMIS.Operation.PPMISNoticeInfo">
<SelectParameters>
<asp:Parameter Name="noticeID" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="ODSTank" runat="server"
OldValuesParameterFormatString="original_{0}" SelectMethod="GetTankItem"
TypeName="PPMIS.Operation.PPMISNoticeInfo">
<SelectParameters>
<asp:Parameter Name="oilID" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
</ContentTemplate>
</asp:UpdatePanel>
{
if(lbTank.options[i].selected)
{
alert(lbTank.options[i].text)
}
}
把这个options[i].value alert出来看看,是不是有相同的?
protected void ManageBoxFormView_ItemInserting(object sender, FormViewInsertEventArgs e)
{
ListBox lbTank = ManageBoxFormView.FindControl("lbTank") as ListBox; //倒出罐
ListBox lbInTank = ManageBoxFormView.FindControl("lbInTank") as ListBox;
string tempstr = "";
for (int i = 0; i < lbTank.Items.Count; i++)
{
if (lbTank.Items[i].Selected)
{
if (string.IsNullOrEmpty(tempstr))
{
tempstr = lbTank.Items[i].Value;
}
else
{
tempstr = tempstr + "," + lbTank.Items[i].Value;//无论选多少只能读到一个数据
}
}
} string[] tempstr3 = Request.Form.GetValues(lbTank.UniqueID);//这个只能显示多个相同的数组 ,调试用的代码
e.Values["NoticeTool"] = tempstr;
}
欢迎个位来群讨论!