按钮单击事件调用时,我不希望网格视图为空 [英] I don't want grid view empty when button click event call
本文介绍了按钮单击事件调用时,我不希望网格视图为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
there is one dropdown and one button and two grid view. when i select dropdown value and than first grid will fill than i select radio button and click on apply button than second grid will be fill. problem is start now again i select drop down it will display first grid and select radio button and click on button and display only new record and old will remove so i want both
protected void btnapply_Click(object sender, EventArgs e)
{
SelectModel();
}
public void SelectModel()
{
if (IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[6] { new DataColumn("Software"), new DataColumn("id"), new DataColumn("module"), new DataColumn("rdoread"), new DataColumn("rdoreadmodify"), new DataColumn("all") });
foreach (GridViewRow row in Gridmodule.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRead = (row.Cells[0].FindControl("rdoread") as CheckBox);
CheckBox chkReadModify = (row.Cells[0].FindControl("rdoreadmodify") as CheckBox);
CheckBox chkAll = (row.Cells[0].FindControl("rdoall") as CheckBox);
if (chkRead.Checked)
{
string id = (row.Cells[1].FindControl("lblid") as Label).Text;
string module = (row.Cells[2].FindControl("lblmodule") as Label).Text;
bool read = true;
bool readmodify = false;
bool all = false;
string soft = (drpsoftware.SelectedItem.ToString());
dt.Rows.Add(soft, id, module, read, readmodify, all);
}
if (chkReadModify.Checked)
{
string id = (row.Cells[1].FindControl("lblid") as Label).Text;
string module = (row.Cells[2].FindControl("lblmodule") as Label).Text;
bool read = false;
bool readmodify = true;
bool all = false;
string soft = (drpsoftware.SelectedItem.ToString());
dt.Rows.Add(soft, id, module, read, readmodify, all);
}
if (chkAll.Checked)
{
string id = (row.Cells[1].FindControl("lblid") as Label).Text;
string module = (row.Cells[2].FindControl("lblmodule") as Label).Text;
bool read = false;
bool readmodify = false;
bool all = true;
string soft = (drpsoftware.SelectedItem.ToString());
dt.Rows.Add(soft, id, module, read, readmodify, all);
}
}
}
gridselected.DataSource = dt;
gridselected.DataBind();
}
i希望当我第一次按下此按钮时它会填满网格,当我再次按下相同的按钮时,它永远不会删除以前的数据并填充新数据老了。
我尝试了什么:
i try viewstat但是可以没有解决问题。
i want that when i press first time this button it fill grid and when i press again the same button it never erase previous data and fill new data with old .
What I have tried:
i try viewstat but can no solve the issue.
推荐答案
参考这个简单的例子并在你的代码中实现..
< big> ASPX:
Refer this simple example and implement in your code..
ASPX:
<asp:GridView ID="Gridmodule" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox Text="" ID="txtSoftware" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:GridView ID="gridselected" runat="server">
<Columns>
<asp:BoundField DataField="Software" HeaderText="Software" ItemStyle-Width="50" />
</Columns>
</asp:GridView>
<asp:Button ID="btnapply" runat="server" Text="Apply" OnClick="btnapply_Click" />
CS
CS
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("SomeColumn");
dt.Rows.Add("one");
dt.Rows.Add("two");
Gridmodule.DataSource = dt;
Gridmodule.DataBind();
}
}
protected void btnapply_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Software");
foreach (GridViewRow row in Gridmodule.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
bool isChecked = (row.FindControl("chk") as CheckBox).Checked;
if (isChecked)
{
string a = (row.FindControl("txtSoftware") as TextBox).Text;
dt.Rows.Add(a);
}
}
}
// Adding the Second grid data to datatable
foreach (GridViewRow row in gridselected.Rows)
if (row.RowType == DataControlRowType.DataRow)
{
string software = row.Cells[0].Text;
// similarly for all columns
dt.Rows.Add(software);
}
gridselected.DataSource = dt;
gridselected.DataBind(); ;
}
这篇关于按钮单击事件调用时,我不希望网格视图为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文