locked
GridView 的pager 格式問題 RRS feed

  • 問題

  •    Protected Sub gdvLawList_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gdvLawList.RowDataBound

                If e.Row.RowType = DataControlRowType.Pager Then
                    Dim pagerCell As TableCell
                    pagerCell = CType(e.Row.Controls(0), TableCell)
                    'pagerCell.Controls.Clear()
                    Dim txt As String
                    Dim txtGoPage As String

                     txtGoPage = "Page No:"

                    txt = "<span><font size='2pt'>" & txtGoPage & "&nbsp;</font><span class=i>"
                    'txt = txtGoPage
                    pagerCell.Controls.AddAt(0, New LiteralControl(txt))
                    pagerCell.Controls.AddAt(pagerCell.Controls.Count, New LiteralControl("</span></span>"))

    End if
    End Sub

    我想把 "Page No: " 和 "1 2 3 4 ..... " 放在同一行

    但現在出來的結果是分成兩行
    Page No:
    1 2 3 4 .....

    有甚麼方法可以放在同一行顯示 ?

    2009年3月30日 上午 09:49

解答

  • 你好

    下面代码应该可以了。

     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GridViewRow gvr = e.Row;
            if (gvr.RowType == DataControlRowType.Pager)
            {
                gvr.ID = "test";
                gvr.Controls[0].ID = "subTest";
    
                Table htc = (gvr.Controls[0].Controls[0] as Table);
    
                TableCell tc = new TableCell();
                tc.Controls.Add(new LiteralControl("Page No"));
                htc.Controls[0].Controls.AddAt(0, tc);          
            }
        }

    Microsoft Online Community Support
    • 已標示為解答 Lam0706 2009年4月3日 上午 09:32
    2009年4月3日 上午 02:28

所有回覆

  • Can you show us the output html from selecting "view source code" in IE?
    大家一齊探討、學習和研究,謝謝! Microsoft MVP, Microsoft Community Star(TW & HK), MCT, MCSD, MCAD, MCSE+I, MCDBA, MCDST, MCSA, MCTS, MCITP
    2009年3月30日 下午 05:42
  • 楼主,你好

    下面的代码我已经测试过了,满足你的要求,你试试。

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) 
        Dim gvr As GridViewRow = e.Row 
        If gvr.RowType = DataControlRowType.Pager Then 
            Dim tc As New TableCell() 
            tc.Controls.Add(New LiteralControl("Page No")) 
            gvr.Cells.AddAt(0, tc) 
        End If 
    End Sub 

    Microsoft Online Community Support
    2009年4月1日 上午 03:20
  • Can you show us the output html from selecting "view source code" in IE?
    大家一齊探討、學習和研究,謝謝! Microsoft MVP, Microsoft Community Star(TW & HK), MCT, MCSD, MCAD, MCSE+I, MCDBA, MCDST, MCSA, MCTS, MCITP
    view source code

    沒有顯示GRIDVIEW Html的CODE, 
    請問為甚麼 ?
                                        <div id="dlg" class="panel" style="width:962px">
                            <div class="header" style="cursor:default">
                                <div class="outer">
                                    <div class="inner">
                                        <div class="content">
                                            <h2>&nbsp;</h2>      
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="body">
                                <div class="outer">
                                    <div class="inner">
                                        <div class="content">
                                       <div id="ctl00_ContentPanel_CollectionLaw1_UpdatePanel_GridView">
     
                                       

                                            <div>

     </div>
                                               
    </div>
                                          </div>
                                    </div>
                                </div>
                            </div>               
                            <div class="footer">
                                <div class="outer">
                                    <div class="inner">
                                        <div class="content"></div>
                                    </div>
                                </div>
                            </div>     
                        </div>
                                
                                </div>
                            </div>
                        </div>

    2009年4月2日 上午 04:36
  • 楼主,你好

    下面的代码我已经测试过了,满足你的要求,你试试。

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) 
    
    
    
        Dim gvr As GridViewRow = e.Row 
    
    
    
        If gvr.RowType = DataControlRowType.Pager Then 
    
    
    
            Dim tc As New TableCell() 
    
    
    
            tc.Controls.Add(New LiteralControl("Page No")) 
    
    
    
            gvr.Cells.AddAt(0, tc) 
    
    
    
        End If 
    
    
    
    End Sub 
    
    
    
    

    Microsoft Online Community Support

    結果影響GridView的內容,
    因為使用了 new TableCell(),
    所以在Pager中加多了一個Cell,
    使Pager上面的資料少了一個Cell, 內容位置變了.

    有沒有其他方法 ?

    ie source html code

    <table class="datatable" cellspacing="0" cellpadding="0" border="0" id="ctl00_ContentPanel_Claw_gdvList" style="color:Black;border-width:0px;font-size:10pt;width:950px;border-collapse:collapse;">
       <tr align="center" valign="middle" style="height:20px;">
        <th align="center" scope="col">Title</th><th align="center" scope="col">aa</th><th scope="col">bb</th><th scope="col">cc</th><th scope="col">dd</th><th align="center" scope="col">ee</th><th scope="col">
                                                        </th>
       </tr><tr class="row" valign="top" style="height:50pt;">
        <td class="first" align="center" style="width:70px;">
                                                             
             
                                                            </td>
           <td align="center" style="width:80px;">                                                       
                                                            </td>
           <td align="center" style="width:60px;">                                                       
                                                        </td>
           <td style="width:150px;">
                                                                  Title
                                                            </td><td align="center" style="width:100px;">
                                                            </td><td style="width:250px;">
                                                             
                                                            </td>
           <td align="center" style="width:20px;">                                                                 
                                                                       
                                                        </td>
       <tr align="center" style="color:Black;background-color:#F7F7DE;">
        <td colspan="7">Page NO:</td><td colspan="13"><table border="0">
         <tr>
          <td><span>1</span></td><td><a href="javascript:__doPostBack('ctl00$ContentPanel$Claw$gdvList','Page$2')" style="color:Black;">2</a></td><td><a href="javascript:__doPostBack('ctl00$ContentPanel$Claw$gdvList','Page$3')" style="color:Black;">3</a></td><td><a href="javascript:__doPostBack('ctl00$ContentPanel$Claw$gdvList','Page$4')" style="color:Black;">4</a></td><td><a href="javascript:__doPostBack('ctl00$ContentPanel$Claw$gdvList','Page$5')" style="color:Black;">5</a></td><td><a href="javascript:__doPostBack('ctl00$ContentPanel$Claw$gdvList','Page$6')" style="color:Black;">6</a></td><td><a href="javascript:__doPostBack('ctl00$ContentPanel$Claw$gdvList','Page$7')" style="color:Black;">7</a></td><td><a href="javascript:__doPostBack('ctl00$ContentPanel$Claw$gdvList','Page$8')" style="color:Black;">8</a></td><td><a href="javascript:__doPostBack('ctl00$ContentPanel$Claw$gdvList','Page$9')" style="color:Black;">9</a></td><td><a href="javascript:__doPostBack('ctl00$ContentPanel$Claw$gdvList','Page$10')" style="color:Black;">10</a></td><td><a href="javascript:__doPostBack('ctl00$ContentPanel$Claw$gdvList','Page$11')" style="color:Black;">...</a></td>
         </tr>
        </table></td>
       </tr>
      </table>
    2009年4月2日 上午 04:46
  • 你好,

    不知道你想要什么样子的,你上面的html代码少个table element吧。
    所以你最好能说明你希望"Page No"放在哪

    Microsoft Online Community Support
    2009年4月2日 上午 06:20
  • 你好,

    不知道你想要什么样子的,你上面的html代码少个table element吧。
    所以你最好能说明你希望"Page No"放在哪

    Microsoft Online Community Support

    我要的結果很簡單, 只是把 "Page No" 和 "1 2 3 4 5 ..."   放在同一行顯示.
    雖然你的代碼可以做到, 但結果內容就走位了.

    你給我的代碼 因為使用new TableCell(), 使Table的其他Row 少了一個TableCell,  
    所以gridview 的內容就不對齊, 走位了.

    2009年4月3日 上午 01:55
  • 你好

    下面代码应该可以了。

     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GridViewRow gvr = e.Row;
            if (gvr.RowType == DataControlRowType.Pager)
            {
                gvr.ID = "test";
                gvr.Controls[0].ID = "subTest";
    
                Table htc = (gvr.Controls[0].Controls[0] as Table);
    
                TableCell tc = new TableCell();
                tc.Controls.Add(new LiteralControl("Page No"));
                htc.Controls[0].Controls.AddAt(0, tc);          
            }
        }

    Microsoft Online Community Support
    • 已標示為解答 Lam0706 2009年4月3日 上午 09:32
    2009年4月3日 上午 02:28
  • 成功了..

    Thanks
    2009年4月3日 上午 09:33