2011-05-22 29 views
0

下午好,asp.net C#,選擇html元素在運行時處理

我有一個html aspx頁面,有許多div元素。這些網格的div元素。網格大小選擇客戶端,div元素通過請求生成服務器端。生成的div元素是這樣的:

<div id="cell_0_0"></div> 
<div id="cell_0_1"></div> 
<div id="cell_0_2"></div> 
<div id="cell_0_3"></div> 

<div id="cell_1_0"></div> 
<div id="cell_1_1"></div> 
etc... 
etc... 

當電網從用戶充滿了整數值,該值可以由服務器訪問,所以服務器需要得到這些元素的值。這很簡單。

int value1 = cell_0_0.innerHTML; 
int value2 = cell_0_1.innerHTML; 

問題是......服務器生成的div的數量是在運行時決定的。 (在客戶端輸入上,例如4x4,5x5..9x9網格)

因此,div可以從div id =「cell_0_0」到div id =「cell_4_4」或從cell_0_0到cell_9_9。

現在我可以在服務器上muliple功能,如:(僞)

if grid is 5x5 
    int value5x5-1 = cell_0_0.innerHTML; 
    ... ... 
    int value5x5-25 = cell_5_5.innerHTML; 

if grid is 9x9 
    int value9x9-1 = cell_0_0.innerHTML; 
    ... ... 
    int value9x9-81 = cell_9_9.innerHTML 

問題是...這些功能將是巨大的!對於一個9x9的網格,有81個單元,這樣就有81個程序輔助!

在JavaScript中,我通過細胞使用for循環

for (var i = 0; i < gridSize; i++) { 
    for (var j = 0; i < gridSize; j++) { 
     document.getElementById("cell_" + i + "_" + j); 
    } 
} 
// as you can see the cell ids are constructed as the loop progresses, because the parameter is a string 

最後迭代...問題是...有什麼辦法?我可以在服務器端的C#代碼動態根據有多少個單元格遍歷單元格變量。

如果不是...什麼是最好的方法來實現這樣的問題?

對不起,長屁股解釋,想確保你確切地知道我在做什麼!

我知道它的一個奇怪的問題,很抱歉,如果有任何混淆:)

謝謝, 亞歷克斯

回答

1

問題是......有沒有什麼辦法......我可以讓服務器端的C#代碼動態地遍歷單元格變量,具體取決於有多少單元格。

第一個問題是,如果你divs沒有runat="server"(如你的例子)那麼c#代碼不能識別元素的實例。第二,爲了方便起見,(如果你還沒有這樣做,),把你所有的'網格div'放到一個包含div的單個文件中。這將有助於在服務器上對您想要的控件進行分組和識別。它最終會看起來像這樣:

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <style type="text/css"> 
     #my_grid 
     { 
      width:400px; 
      float:left; 
     } 
     #my_grid div 
     { 
      width:93px; 
      border:1px solid blue; 
      float:left; 
      text-align:right; 
      padding-right:5px; 
     } 
    </style> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div id="my_grid" runat="server"> 
     <div id="cell_0_0" runat="server">2</div> 
     <div id="cell_0_1" runat="server">3</div> 
     <div id="cell_0_2" runat="server">5</div> 
     <div id="cell_0_3" runat="server">8</div> 

     <div id="cell_1_0" runat="server">13</div> 
     <div id="cell_1_1" runat="server">21</div> 
     <div id="cell_1_2" runat="server">34</div> 
     <div id="cell_1_3" runat="server">65</div> 


     <div id="cell_2_0" runat="server">99</div> 
     <div id="cell_2_1" runat="server">164</div> 
     <div id="cell_2_2" runat="server">263</div> 
     <div id="cell_2_3" runat="server">427</div> 

     <div id="cell_3_0" runat="server">690</div> 
     <div id="cell_3_1" runat="server">1117</div> 
     <div id="cell_3_2" runat="server">1807</div> 
     <div id="cell_3_3" runat="server">2914</div> 
</div> 
    <asp:Button ID="Button1" runat="server" Text="Click Me" 
     onclick="Button1_Click" /><br /> <asp:Button ID="Button2" runat="server" 
     Text="Click Me 2" onclick="Button2_Click" /> 
    </form> 
</body> 
</html> 

(爲4x4的方格,你的問題只介紹了一個AXA電網的可能性,而不是AXB格,我會用這樣的假設工作):

最後,一旦你回到服務器(大概在回發期間,但你可以在完成創建網格後調用它),你可以收集所有的div在一個lamda選擇(在這種情況下,我們是使用「OfType」和「First」來選擇所需的實例),然後從這裏與他們一起工作:

public partial class selecting_divs_with_linq : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    protected void Button1_Click(object sender, EventArgs e) 
    { 
     //When I get your div value I am going to put it in this list...I don't know what you are going to with it... 
     List<double> results = new List<double>(); 

     //First we want to get a list of all of the DIV inside of the container div for my_grid: 
     List<System.Web.UI.HtmlControls.HtmlGenericControl> listOfDivs = this.my_grid.Controls.OfType<System.Web.UI.HtmlControls.HtmlGenericControl>().ToList<System.Web.UI.HtmlControls.HtmlGenericControl>(); 
     //because the array is AxA we need the square root of the count of divs and this gives us the upper range of the array values: 
     double A = Math.Sqrt(listOfDivs.Count); 

     //these nested for loops extract every value from your grid and puts them into the "results" list 
     //of course if you want to do something else with them that works too... see button2_click for an alternative... 
     for (double X = 0; X < A; X++) 
     { 
      for (double Y = 0; Y < A; Y++) 
      { 
       string divId = string.Format("cell_{0}_{1}", Y.ToString(), X.ToString()); 
       var div = listOfDivs.First(d => d.ID == divId); 
       //now you have 'the div' 
       string dblX = div.InnerText; 
       results.Add(double.Parse(dblX)); 

      } 
     } 
    } 

    protected void Button2_Click(object sender, EventArgs e) 
    { 
     //First we want to get a list of all of the DIV inside of the container div for my_grid: 
     List<System.Web.UI.HtmlControls.HtmlGenericControl> listOfDivs = this.my_grid.Controls.OfType<System.Web.UI.HtmlControls.HtmlGenericControl>().ToList<System.Web.UI.HtmlControls.HtmlGenericControl>(); 
     //because the array is AxA we need the square root of the count of divs and this gives us the upper range of the array values: 
     double A = Math.Sqrt(listOfDivs.Count); 

     //these nested for loops extract every value from your grid and puts them into the "results" list 
     //of course if you want to do something else with them that works too... see button2_click for an alternative... 
     for (double X = 0; X < A; X++) 
     { 
      for (double Y = 0; Y < A; Y++) 
      { 
       string divId = string.Format("cell_{0}_{1}", Y.ToString(), X.ToString()); 
       var div = listOfDivs.First(d => d.ID == divId); 
       //now you have 'the div' 
       string dblX = div.InnerText; 
       //alternate: update the value of each entry like this: 
       div.InnerHtml = (double.Parse(dblX)/2).ToString(); 
       //now each value gets cut in half 

      } 
     } 
    } 
} 
0
我能想到的最簡單的

的解決辦法是把申報單的值轉換爲二維數組javascript,然後將該數組以JSON格式傳回服務器。服務器代碼然後將JSON解析成C#數組,然後可以從那裏做你想做的事。

0

你總是可以做這樣的事情來解析所有id爲「cell_」開頭的控件。

foreach (Control control in controlThatContainsTheDivs.Controls) 
{ 
    if (control.ID != null && control.ID.StartsWith("cell_")) 
    { 
     //do what you need with it's value 
    } 
} 

注意,爲了讓這樣的事情工作,div的必須要麼被解析出持有它們,字面控制的內部HTML,或者你必須添加RUNAT =「服務器」標籤所以控制集合可以看到它們。

該示例的HTML上面是這樣

<div id="controlThatContainsTheDivs" runat="server"> 
    <div id="cell_0_0" runat="server">1</div> 
    <div id="cell_0_1" runat="server">2</div> 
    <div id="cell_0_2" runat="server">3</div> 
    <div id="cell_0_3" runat="server">4</div> 
    <div id="cell_1_0" runat="server">5</div> 
    <div id="cell_1_1" runat="server">6</div> 
</div> 

如果一定要知道在標識的數字是很重要的,它應該是很容易分析出。

我希望這會有所幫助。