GridView和updatepanel-不要更改视图

本文关键字:视图 updatepanel- GridView | 更新日期: 2023-09-27 17:57:49

我需要更新面板的帮助。我有一个这样的网格:

  <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>

所以当我点击选择按钮时,他需要更改选择并刷新更新面板。因此:

 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++)
                    {
                    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);
    }

它适用于首次选择或从不。。。如果我按几次F5,它又开始工作了。我应该改变什么?

GridView和updatepanel-不要更改视图

为什么要设置EnableViewState="false"?单击该按钮时,不会显示网格视图数据,因为在page PostBack上,由于您将其设置为false,因此不会维护viewState。但当你按F5时,整个页面会刷新,它会得到数据。

只需从更新面板中删除EnableViewState="false",它就可以工作了。

其次,在!IsPostBack下的page_load中设置以下条件:

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();
            }

}