asp.net给Reaper和GridView添加序号

repeater添加序号列的方法 

1、<itemtemplate> 
<tr><td> 
<%# Container.ItemIndex + 1%> 
</td></tr> 
</itemtemplate> 

2、<itemtemplate> 
<tr><td> 
<%# this.rpResult.Items.Count + 1%> 
</td></tr> 
</itemtemplate> 

3、在<form></form>中添加<Label ID="dd" ></Label> 
<body nload="show()"> 
<Script. Language="JScript"> 
function show() 
{ 
var bj = document.all.tags("LABEL"); 
for (i=0;i<obj.length;i++) 
{ 
document.all["dd"][i].innerHTML=i+1; 
} 
} 
</script> 

4、后台实现方法 
在.aspx里添加<asp:Label id="Label1" Runat="server"></asp:Label> 
在.cs里添加 
** void InitializeComponent() 
{ 
this.Repeater1.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(this.Repeater1_ItemDataBound); 
this.Load += new System.EventHandler(this.Page_Load); 
} 
** void Repeater1_ItemDataBound(object source, System.Web.UI.WebControls.RepeaterItemEventArgs e) 
{ 
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
{ 
((Label)e.Item.FindControl("Label1")).Text = Convert.ToString(e.Item.ItemIndex + 1); 
} 
} 
5.根据条件添加html 
<%#Container.ItemIndex == 8 ? "<br><a href = 'http://www.ginchan.com.tw/' target='_blank'><img style='width:338px;heigh:70px' src='/ImportAD/ADmid.gif'> </a>" : ""%>

 

ASP.NET repeater添加序号列的方法

1.第一种方式

直接在Aspx页面GridView模板列中.这种的缺点是到第二页分页时又重新开始了.
<asp:TemplateField HeaderText="序号" InsertVisible="False">                       
    <ItemTemplate>               
        <%#Container.DataItemIndex+1%>            
    </ItemTemplate>             
</asp:TemplateField>

2.第二种方式

分页时进行了计算,这样会累计向下加
<asp:TemplateField HeaderText="序号" InsertVisible="False">
           <ItemStyle HorizontalAlign="Center" />
           <HeaderStyle HorizontalAlign="Center"/>
            <ItemTemplate>
                   <asp:Label ID="Label2" runat="server" Text='<%# this.GridView1.PageIndex * this.GridView1.PageSize + this.GridView1.Rows.Count + 1%>' />
           </ItemTemplate>
</asp:TemplateField>

3.第三种方式

放在cs代码中,和第二种相似
<asp:BoundField HeaderText="序号" ></asp:BoundField>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)         
{             
    if (e.Row.RowIndex != -1)             
    {                 
        int indexID = this.GridView1.PageIndex * this.myGridView.PageSize + e.Row.RowIndex + 1;                 
        e.Row.Cells[0].Text = indexID.ToString();             
    }         
}

 

4.不用添加在后台查找

int rowIndex = ((GridViewRow)((Button)e.CommandSource).NamingContainer).RowIndex;
TextBox tb = (TextBox)GridView1.Rows[i].FindControl("TextBox");
e.CommandSource传的是按下去的Button,不过由于传回的是Object,就得自行转化Button,但我们想知道的是RowIndex,而Button是包含在GridViewRow內,所以通过NamingContainer传回目前的GridViewRow,得到是Control,所以需在转成GridViewRow后才能有RowIndex 属性。
原理是:将当前行索引和Button的commandargument绑定,用的时候只要取出当前行的索引即可,gridview的rowcommand事件和datalist的itemcommand事件相似

在repeater中查找当前的行方法如下:

 

    protected void repQuestions_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Button butEdit = (Button)(e.Item.FindControl("butEdit"));
            Button butDelete = (Button)(e.Item.FindControl("butDelete"));
            Label labQuetionType = (Label)(e.Item.FindControl("labQeustionType"));
            TextBox texSequence = (TextBox)(e.Item.FindControl("texSequence"));
            int cursequence = Convert.ToInt32(butEdit.CommandArgument);
            int id = Convert.ToInt32(butDelete.CommandArgument);
            QuestionType questionType = questionnaireService.GetQuestionType(labQuetionType.Text);
            if(e.CommandName == "Edit")
            {
                EditCurrentQuestion(id, cursequence);
                hidQuestionSequence.Value = texSequence.Text.Trim();
            }
            else if(e.CommandName == "Delete")
            {
                int index = questionnaire.Questions.FindIndex(a => a.Sequence == cursequence);
                this.questionnaire.Questions.RemoveAt(index);
            }
            BindQuestions();
        }
    }

 这里涉及到在gridview的某一个单元格里查找值的问题,这个方法很多了,但是有一种很特别,如果在前台绑定用TemplateField,但是里面却用Eval,这样应该怎么查找呢,答案是DataBoundLiteralControl,这个空间就是这个单元格类型控件,他的Text属性就是我们要查找的值,前台绑定代码和后台的查找方法如下:

 

前台绑定

<asp:TemplateField HeaderText="回复数">
    <ItemTemplate>
    <%#Eval("ReplyCount")%>
    </ItemTemplate>
</asp:TemplateField>

后台获取

protected void gvStatistic_RowCommand(object sender, GridViewCommandEventArgs e)
{
    int id = Convert.ToInt32(((LinkButton)e.CommandSource).CommandArgument);
    int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
    if(e.CommandName == "check")
    {
        string replyCount = ((DataBoundLiteralControl)(gvStatistic.Rows[rowIndex].Cells[2].Controls[0])).Text.Trim();
        if(!string.IsNullOrWhiteSpace(replyCount) && replyCount == "0")
        {
            AlertMessage.Show(this, "该问卷没有回复!");
        }
        else
        {
            Server.Transfer("StatisticsDetails.aspx?ID=" + id.ToString());
        }
    }

 

 

 

 

posted @ 2013-04-01 22:19  nd  阅读(1784)  评论(0编辑  收藏  举报