我有一个想要显示的按钮 gridview
以前它是根据 Datatable
但是现在我想在 Array
这是代码
protected void btnSubmit_Click(object sender, EventArgs e)
{
var selectedItems = cmbEmp_Name.Items.Cast<ListItem>().Where(x => x.Selected).Select(x => x.Value).ToArray();
var result = String.Join(",", selectedItems);
string[] StrAreaInt = result.Split(new char[] { ',' });
for (int i = 0; i < StrAreaInt.Length; i++)
{
Muster_Process(StrAreaInt[i]); // length comes here
HiddenDiv.Visible = true;
grdMonthlyProc.Visible = true;
}
}
这些是我认为您正在使用的控件。
<asp:CheckBoxList ID="cmbEmp_Name" runat="server" RepeatLayout="Flow"></asp:CheckBoxList>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:GridView ID="grdMonthlyProc" runat="server" AutoGenerateColumns="true"></asp:GridView>
确保仅分配一次数据源,否则您将在每次回发中丢失所选的项目。
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// Changes these to your field names
cmbEmp_Name.DataTextField= "Name";
cmbEmp_Name.DataValueField = "DoctorID";
// This code populate the CheckBoxList in my own project
// Your Post doesn't show how you populate your control
// I assume it's being done elsewhere in your code.
// But it is important to understand that if you do not load
// the CheckBoxList within a `if (!Page.IsPostBack)` block anything
// you select in the list will be lost when you click the button
// because the control will be repopulated on Postback.
cmbEmp_Name.DataSource = ef6Context.GetListOfNames().ToList();
cmbEmp_Name.DataBind();
}
}
//Button1 Postback bind the Gridview.
protected void Button1_Click(object sender, EventArgs e)
{
// Once you select a number of items from `cmbEmp_Name` and hit your
// button this statement will create and Array of `ListItem`s
grdMonthlyProc.DataSource = cmbEmp_Name.Items
.Cast<ListItem>()
.Where(li => li.Selected)
.ToArray();
// And this will Bind the Array to the GridView. Internally GridView
// will extract all Public Properties of a ListItem and automatically
// generate a default set of Columns to display. With a simple
// redefinition you can display specific columns, but more on that later
grdMonthlyProc.DataBind();
}
您的帖子中的代码块做了太多的工作:
Value
Value
Value
然后您有一个for循环。我不知道为什么,但是您打电话给Muster_Process,我认为这是相关的,但是您不说它的作用。然后,您将“可见性”重复设置为循环内的相同控件(HiddenDiv
和grdMonthlyProc
),这毫无用处。在循环外一次就足够了。
但是,在帖子的第一行中,您提到要填充GridView,但是没有说要显示的内容。
定义如下grdMonthlyProc
将显示ListItems的所有公共属性:
<asp:GridView ID="grdMonthlyProc" runat="server" AutoGenerateColumns="true">
</asp:GridView>
重新定义为此(请注意此更改AutoGenerateColumns="false"
:),它将仅显示Value属性:
<asp:GridView ID="grdMonthlyProc" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Value" HeaderText="Value" />
</Columns>
</asp:GridView>
并且添加其他内容BoundField
将仅显示“值”字段和“文本”字段:
<asp:GridView ID="grdMonthlyProc" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Value" HeaderText="Value" />
<asp:BoundField DataField="Text" HeaderText="Text" />
</Columns>
</asp:GridView>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句