2011-06-22 93 views
0

我有一個按鈕,然後在我的asp.net網站page.I一個的GridView給在C#代碼下面的代碼隱藏文件尋呼GridView控件在asp.net

protected void Button1_Click1(object sender, EventArgs e) 
{ 

    string t = @"<countries> 
      <country> 
      <name>ANGOLA</name><code>1</code><size>1345 amp</size> 
      </country> 
      <country> 
      <name>ANGOLA</name><code>2</code><size>1345 amp</size> 
      </country> 
      <country> 
      <name>ANGOLA</name><code>3</code><size>1345 amp</size> 
      </country> 
      <country> 
      <name>ANGOLA</name><code>4</code><size>1345 amp</size> 
      </country> 
      <country> 
      <name>ANGOLA</name><code>5</code><size>1345 amp</size> 
      </country> 
      <country> 
      <name>ANGOLA</name><code>6</code><size>1345 amp</size> 
      </country> 
      <country> 
      <name>ANGOLA</name><code>24</code><size>1345 amp</size> 
      </country> 
      <country> 
      <name>ANGOLA</name><code>24</code><size>1345 amp</size> 
      </country> 
      <country> 
      <name>ANGOLA</name><code>24</code><size>1345 amp</size> 
      </country> 
      <country> 
      <name>ANGOLA</name><code>24</code><size>1345 amp</size> 
      </country> 
      <country> 
      <name>ANGOLA</name><code>24</code><size>1345 amp</size> 
      </country> 
      <country> 
      <name>ANGOLA</name><code>24</code><size>1345 amp</size> 
      </country> 
      <country> 
      <name>ANGOLA</name><code>24</code><size>1345 amp</size> 
      </country> 
      <country> 
      <name>ANGOLA</name><code>24</code><size>1345 amp</size> 
      </country> 

      <country> 
      <name>BENIN</name><code>204</code><size>435 amp</size> 
      </country> 
      </countries>"; 
    //string bgtFocusCmd = "<bgfocuscmd >"; 
    //string countCmd = "<count name='" + Session["operation"] + "'customerid='" + customerid.Text + "' breakup='" + breakup + "' date='" + DateFrom.Text + "' >"; 
    //bgtFocusCmd += countCmd + "</bgfocuscmd>"; 
    XmlDocument doc = new XmlDocument(); 
    doc.LoadXml(t); 
    DataSet resultData = new DataSet(); 
    resultData.ReadXml(new StringReader(doc.OuterXml)); 
    dataGrid.DataSource = resultData.Tables[0].DefaultView; 
    dataGrid.DataBind(); 

} 

和aspx頁面

<asp:GridView ID="dataGrid" runat="server" 
     AllowPaging="True" 
     AutoGenerateColumns="True" 
     CellPadding="4" 
     DataSourceID="Button1_Click1.t" 
     EmptyDataText="NO data available." 
     EnableSortingAndPagingCallbacks="true" 
     ForeColor="#333333" 
     GridLines="None" 
     Height="302px" 
     HorizontalAlign="Left" 
     PageSize="2" 
     RowStyle-Width="20" 
     Width="560px" 
     OnPageIndexChanged="Button1_Click1"> 
    <RowStyle BackColor="#EFF3FB" />      
    <FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" /> 
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> 
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> 
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
    <EditRowStyle BackColor="#2461BF" /> 
    <AlternatingRowStyle BackColor="White" /> 
</asp:GridView> 

當我點擊button1時,它將顯示兩行的gridview。 但是,當我點擊網格視圖中的pagenumbers喜歡點擊頁碼2時,它將顯示沒有可用的數據。我想在網格視圖中顯示該頁面。任何人都可以告訴如何做到這一點,真的很感激。 謝謝

回答

0

您需要添加的狀態remeber您喜歡以顯示最後一次數據,而不是讓數據綁定在頁面的變化,只是給數據。嘗試這個。

const string cRemStateNameConst = "cRemState_cnst"; 

public int cRemState 
{ 
    set 
    { 
     if (value == -1) 
      ViewState.Remove(cRemStateNameConst); 
     else 
      ViewState[cRemStateNameConst] = value; 
    } 
    get 
    { 
     if (ViewState[cRemStateNameConst] is int) 
      return (int)ViewState[cRemStateNameConst]; 
     else 
      return -1; 
    } 
} 



protected void Page_Load(object sender, EventArgs e) 
{ 
    if(cRemState == 1) 
     GetTheData(); 
} 

protected void Button1_Click1(object sender, EventArgs e) 
{ 
    cRemState = 1; 
    GetTheData(); 
    dataGrid.DataBind(); 
} 

void GetTheData() 
{ 
    string t = @"<countries> 
      <country> 
      <name>ANGOLA</name><code>1</code><size>1345 amp</size> 
      </country> 
      <country> 
      <name>ANGOLA</name><code>2</code><size>1345 amp</size> 
      </country> 
      <country> 
      <name>ANGOLA</name><code>3</code><size>1345 amp</size> 
      </country> 
      <country> 
      <name>ANGOLA</name><code>4</code><size>1345 amp</size> 
      </country> 
      <country> 
      <name>ANGOLA</name><code>5</code><size>1345 amp</size> 
      </country> 
      <country> 
      <name>ANGOLA</name><code>6</code><size>1345 amp</size> 
      </country> 
      <country> 
      <name>ANGOLA</name><code>24</code><size>1345 amp</size> 
      </country> 
      <country> 
      <name>ANGOLA</name><code>24</code><size>1345 amp</size> 
      </country> 
      <country> 
      <name>ANGOLA</name><code>24</code><size>1345 amp</size> 
      </country> 
      <country> 
      <name>ANGOLA</name><code>24</code><size>1345 amp</size> 
      </country> 
      <country> 
      <name>ANGOLA</name><code>24</code><size>1345 amp</size> 
      </country> 
      <country> 
      <name>ANGOLA</name><code>24</code><size>1345 amp</size> 
      </country> 
      <country> 
      <name>ANGOLA</name><code>24</code><size>1345 amp</size> 
      </country> 
      <country> 
      <name>ANGOLA</name><code>24</code><size>1345 amp</size> 
      </country> 

      <country> 
      <name>BENIN</name><code>204</code><size>435 amp</size> 
      </country> 
      </countries>"; 
    //string bgtFocusCmd = "<bgfocuscmd >"; 
    //string countCmd = "<count name='" + Session["operation"] + "'customerid='" + customerid.Text + "' breakup='" + breakup + "' date='" + DateFrom.Text + "' >"; 
    //bgtFocusCmd += countCmd + "</bgfocuscmd>"; 
    XmlDocument doc = new XmlDocument(); 
    doc.LoadXml(t); 
    DataSet resultData = new DataSet(); 
    resultData.ReadXml(new StringReader(doc.OuterXml)); 
    dataGrid.DataSource = resultData.Tables[0].DefaultView; 

} 

我在這裏所做的。我添加了一個視圖狀態來重新顯示您想要顯示的最後一個數據。因此,在按下按鈕並按下頁面之後,頁面會記住再次調用相同的數據,但在頁面加載時沒有數據,所以頁面將會改變。一個想做的事情是,如果用戶再次按下按鈕,則重置尋呼機。

+0

它正確地給予解決。但是我需要幫忙回覆。請告訴我可以嗎? – sanakkian

+0

@sanakkian我不明白。你能再次告訴我你想要什麼嗎? – Aristos

+0

我需要從webservice獲取數據。所以我需要做ISPOSTBack事件。如果是回傳錯誤,意味着當點擊按鈕時,來自webservice的數據到達datagrid。如果爲true,表示數據從頁面索引值獲得。你能幫我做這件事嗎? – sanakkian

0

在頁面加載使用Button1_Click1(dataGrid, EventArgs.Empty);。當頁索引改變它不會給該方法..