I need a help with updatepanel. I have a grid like this:
<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%" AutoGenerateSelectButton="True"
BorderWidth="1px" EnableSortingAndPagingCallbacks="True"
onselectedindexchanging="GridView1_SelectedIndexChanging"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="nazwa_dysku" HeaderText="Nazwa Dysku"
SortExpression="nazwa_dysku" />
<asp:BoundField DataField="folder" HeaderText="Folder"
SortExpression="folder" />
<asp:BoundField DataField="nazwa_pliku" HeaderText="Nazwa Pliku"
SortExpression="nazwa_pliku" />
<asp:BoundField DataField="czas_trwania" HeaderText="Czas trwania"
SortExpression="czas_trwania" />
<asp:BoundField DataField="rozmiar" HeaderText="Rozmiar"
SortExpression="rozmiar" />
<asp:BoundField DataField="data_utworzenia" HeaderText="Data utworzenia"
SortExpression="data_utworzenia" />
<asp:BoundField DataField="data_modyfikacji" HeaderText="Data modyfikacji"
SortExpression="data_modyfikacji" />
<asp:BoundField DataField="sciezka" HeaderText="Lokalizacja"
SortExpression="sciezka" />
</Columns>
<HeaderStyle BackColor="#9A6E71" Font-Size="Medium" />
<SelectedRowStyle BackColor="Red" />
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridView1" EventName="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlID="GridView1" EventName="SelectedIndexChanging" />
</Triggers>
</asp:UpdatePanel>
So when I click on select button he need to change selection and refresh the updatepanel. So:
public bool dane(int numerdysku)
{
string connectionString = "data source=localhost ;initial catalog=archiwizacjatvs;user id=root;password=wsti";
MySqlConnection conn = new MySqlConnection(connectionString);
try
{
conn.Open();
MySqlDataAdapter dadapter = new MySqlDataAdapter("select nazwa_dysku,folder,nazwa_pliku,czas_trwania,rozmiar,data_utworzenia,data_modyfikacji,sciezka from archiwum where nazwa_dysku='" + DropDownList2.SelectedValue.ToString() + "'", conn);
DataTable tablica = new DataTable();
dadapter.Fill(tablica);
DataTableReader datatablereader = tablica.CreateDataReader();
//DataRow row;
if (tablica.Rows.Count > 0)
{
/*for (int i = 0; i <= tablica.Rows.Count-1; i++)
{
row = tablica.Rows[i];
TextBox2.Text = row[1].ToString() + " : " + row[2].ToString() + " : " + row[3].ToString() + " : " + row[4].ToString() + " : " + row[5].ToString();
}*/
GridView1.DataSource = tablica;
GridView1.DataBind();
conn.Close();
}
else
{
TextBox2.Text = "Tablica jest pusta!";
}
conn.Close();
}
catch (Exception ex)
{
TextBox2.Text = ex.ToString();
}
return true;
}
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = "data source=localhost ;initial catalog=archiwizacjatvs;user id=user;password=password";
MySqlConnection conn = new MySqlConnection(connectionString);
try
{
conn.Open();
MySqlDataAdapter dadapter = new MySqlDataAdapter("select * from archiwum order by nazwa_dysku DESC", conn);
DataTable tablica = new DataTable();
dadapter.Fill(tablica);
DataTableReader datatablereader = tablica.CreateDataReader();
DataRow row = tablica.Rows[0];
for (int i = 1; i <= Convert.ToInt64(row[1]); i++)
{
开发者_JAVA百科 DropDownList2.Items.Add(i.ToString());
}
conn.Close();
}
catch (Exception ex)
{
TextBox2.Text = ex.ToString();
}
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
dane(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)
{
dane(DropDownList2.SelectedIndex);
}
Its working with first select or never... If I press few times F5 its working again. What I should change?
Why would you set EnableViewState="false"
? When you click the button, your Gridview Data is not shown because on the page PostBack
your viewState
is not maintained, since you set it to false. But when you Press F5, then the whole page refreshes and it will get the data.
Just remove EnableViewState="false"
from the updatepanel and it will work.
Secondly, put the following condition in your page_load under !IsPostBack
:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
string connectionString = "data source=localhost ;initial catalog=archiwizacjatvs;user id=user;password=password";
MySqlConnection conn = new MySqlConnection(connectionString);
try
{
conn.Open();
MySqlDataAdapter dadapter = new MySqlDataAdapter("select * from archiwum order by nazwa_dysku DESC", conn);
DataTable tablica = new DataTable();
dadapter.Fill(tablica);
DataTableReader datatablereader = tablica.CreateDataReader();
DataRow row = tablica.Rows[0];
for (int i = 1; i <= Convert.ToInt64(row[1]); i++)
{
DropDownList2.Items.Add(i.ToString());
}
conn.Close();
}
catch (Exception ex)
{
TextBox2.Text = ex.ToString();
}
}
精彩评论