Hello,
I am a newbie in Asp.net technology I need someone's help. I have next situation:
A gridview, two dropdownlists and a button, all of them in a UpdatePanel. When the selection from the first ddl changes, a request is made in MS SQL with LINQ to load data in the second ddl. When the button is pressed another request in MS SQL is made to get information to load in gridview. The problem is: I can't find the manner to create the Datatable to store the data from database when I press that button. Everytime the datatable has the last value, on every click event a new instance of data table is made. How can I solve this issue.
This a part of my code:
I am a newbie in Asp.net technology I need someone's help. I have next situation:
A gridview, two dropdownlists and a button, all of them in a UpdatePanel. When the selection from the first ddl changes, a request is made in MS SQL with LINQ to load data in the second ddl. When the button is pressed another request in MS SQL is made to get information to load in gridview. The problem is: I can't find the manner to create the Datatable to store the data from database when I press that button. Everytime the datatable has the last value, on every click event a new instance of data table is made. How can I solve this issue.
This a part of my code:
public partial class Blabla: BasePage
{
private DataTable _table = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
_table = new DataTable("Table");
_table .Columns.Add(new DataColumn("Col1",typeof(string)));
_table .Columns.Add(new DataColumn("Col2", typeof (string)));
_table .Columns.Add(new DataColumn("Col3", typeof (string)));
gridI.DataSource = _table ;
gridI.DataBind();
}
}
protected void btAdd_Click(object sender, EventArgs e)
{
var list = RequestDB();
DataRow row_n = ReturnANewDataTableRow();
row_n["Col1"] = (_table.Rows.Count + 1).ToString();
row_n["Col2"] = list[0].Filed1.ToString();
row_n["Col3"] = list[0].Field2.ToString();
_table.Rows.Add(row_n);
gridI.DataSource = _table;
gridI.DataBind();
}
}
<asp:UpdatePanel runat="server" ID="UpdatePanel1" ChildrenAsTriggers="true" UpdateMode="Conditional">
<ContentTemplate>
<table width="100%">
<tr>
<td style="width: 353px">
<asp:DropDownList ID="ddlI" runat="server" Width="560px" AutoPostBack="True"
onselectedindexchanged="ddlI_SelectedIndexChanged" >
</asp:DropDownList>
<br/>
<asp:DropDownList ID="ddlD" runat="server" Width="560px"
onselectedindexchanged="ddlD_SelectedIndexChanged" >
</asp:DropDownList>
</td>
</td>
<td style="width: 154px">
<asp:Button ID="btAdd" runat="server" onclick="btAdaugaAbatere_Click"
Text="Add" Width="100px" />
</td>
</tr>
<tr>
<td colspan="3">
<asp:Panel ID="panelC" runat="server">
<asp:GridView ID="gridI" runat="server" Width="100%">
</asp:GridView>
</asp:Panel>
</td>
</tr>
</table>
</td>
</ContentTemplate>
</asp:UpdatePanel>