Assistance needed for implementing an UpdatePanel. Here is the grid structure:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" EnableViewState="false" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownList2" EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<asp:TextBox ID="TextBox2" runat="server" TextMode="MultiLIne" Width="294px"></asp:TextBox>
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
BackColor="#FFFFCC" Width="100%" AutoGenerateSeletButton="True"
BorderWidth="1px" EnableSortingAndPagingCallbacks="True"
onselectedIndexchanging="GridView1_SelectedIndexChanging"
on_SelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="name_disk" HeaderText="Disk Name" SortExpression="disk_name" />
<asp:BoundField DataField="folder" HeaderText="Folder" SortExpression="folder" />
<asp:BoundField DataField="file_namew" HeaderText="File Name" SortExpression="filename" />
<asp:BoundField DataField="duration_time" HeaderText="Duration Time" SortExpression="duration_time" />
<asp:BoundField DataField="size" HeaderText="Size" SortExpression="size" />
<asp:BoundField DataField="creation_date" HeaderText="Creation Date" SortExpression="creation_date" />
<asp:BoundField DataField="modification_date" HeaderText="Modification Date" SortExpression="modification_date" />
<asp:BoundField DataField="path" HeaderText="Location" SortExpression="location" />
</Columns>
<HeaderStyle BackColor="#9A6E71" Font-Size="Medium" />
<SellectedRowStyle BackColor="Red" />
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridView1" EventName="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlID="GridView1" EventName="SelectedIndexChanging" />
</Triggers>
</asp:UpdatePanel>
When selecting a row, it should update and refresh the UpdatePanel. The method in question:
public bool getData(int diskNumber)
{
string connectionString = "data source=localhost ;initial catalog=archiwizacjatvs;user id=root;password=wsti";
MySqlConnection conn = new MySqlConnection(connectionString);
try
{
conn.Open();
MySqlDataAdapter dataAdapter = new MySqlDataAdapter("select name_disk, folder, file_name, duration_time, size, creation_date, modification_date, path from archive where name_disk='" + DropDownList2.SelectedValue.ToString() + "'", conn);
DataTable table = new DataTable();
dataAdapter.Fill(table);
DataTableReader dataReader = table.CreateDataReader();
if (table.Rows.Count > 0)
{
GridView1.DataSource = table;
GridView1.DataBind();
conn.Close();
}
else
{
TextBox2.Text = "Table is empty!";
}
conn.Close();
}
catch (Exception ex)
{
TextBox2.Text = ex.ToString();
}
return true;
}
protected void Page_Load(object sender, EventArgs e)
{
// code here to load initial data into DropDownList2
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
getData(DropDownList2.SelectedIndex);
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridView1.SelectedIndex = e.NewSelectedIndex;
TextBox2.Text = GridView1.SelectedIndex.ToString();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
getData(DropDownList2.SelectedIndex);
}
The current behavior is inconsistent - it works on first select or sometimes not at all. Refreshing the page (F5) multiple times seems to fix the issue temporarily. What changes are recommended?