posts - 7,  comments - 3,  trackbacks - 0
  2008年8月18日

首先绑定DataKeyNames
GridView.DataKeyNames = new string[] { "
字段名称" };
取值

string aaa= GridView.DataKeys[e.Row.RowIndex].Value.ToString();

1
、在绑定时设置主键:
//设置主键;

      
  GridView1.DataKeyNames = new string[] { "id" };
        GridView1.DataBind();
2、获取键值:

 GridView1.DataKeys[e.RowIndex].Value.ToString();

按钮事件里添加
string str_dk= GridView1.DataKeys[e.Row.RowIndex].Value.ToString();

如果GridView控件的DataKeyNames属性设置了表的两个字段(SubjectID,Mode),那么在为GridView绑定数据时对库表的每个记录的指定字段值要作出判断。后台代码中的e.Row.RowIndex即表示GridView的当前行(对应于库表的当前记录);而 View1.DataKeys[e.Row.RowIndex].Values["Mode"].ToString()则获取当前行指定字段(Mode) 的值。若前台代码只写:DataKeyNames="SubjectID'(DataKeyNames属性只填入一个字段名),则后台只需写成: View1.DataKeys[e.Row.RowIndex].Value.ToString()) 即可。
  同样的,要动态获取当前行另一个字段的值可以这样写: View.DataKeys[e.Row.RowIndex].Values["SubjectID"].ToString())

posted @ 2008-08-18 14:08 reddyone 阅读(8969) | 评论 (3)编辑 收藏
  2008年7月26日

 

int a = 5;
int b = 2;

lbl.Text 
= Convert.ToString(Math.Ceiling((double)a / (double)b));
posted @ 2008-07-26 20:46 reddyone 阅读(4325) | 评论 (0)编辑 收藏

<asp:TemplateField>
    
<ItemTemplate>
         
<asp:Label ID="lblNun" Text='<%# Container.DataItemIndex+1  %>' runat="server"></asp:Label>
    
</ItemTemplate>
</asp:TemplateField>
posted @ 2008-07-26 14:02 reddyone 阅读(356) | 评论 (0)编辑 收藏
GridView:
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("MailID") %>' CommandName="DeleteMail" 
        OnClientClick="return confirm('是否删除?');">删除</asp:LinkButton>


页面使用:
1.关闭时提示“是否关闭”
Response.Write("<script language:javascript>javascript:window.close();</script>");
2.直接关闭不提示
Response.Write("<script language:javascript>javascript:window.opener=null;window.close();</script>");
posted @ 2008-07-26 09:18 reddyone 阅读(876) | 评论 (0)编辑 收藏
  2008年7月13日

1. 绑定方法

 公共类(C#)

 1public class Common
 2{
 3    public Common()
 4    {
 5        //
 6        // TODO: 在此处添加构造函数逻辑
 7        //
 8    }

 9
10    //根据要求返回特定长度的字符串
11    public string GetStringByLength(string Str, int Length)
12    {
13        if (Str.Length > Length)
14            return Str.Substring(0, Length);
15        else if (Str.Length > 0)
16            return Str;
17        else
18            return "";
19    }

20
21    //根据要求返回精简的文本
22    public string GetShowText(string Text,int MaxLength)
23    {
24        if (Text.Length < MaxLength)
25            return Text;
26        else
27            return GetStringByLength(Text, MaxLength - 2+ "";
28    }

29
30
31    //判断日期是否是新的
32    public bool IsNew(DateTime Date)
33    {
34        TimeSpan days = DateTime.Now.Subtract(Date);
35
36        int newDays = Int32.Parse(ConfigurationManager.AppSettings["NewDays"].ToString());
37
38        if (days.Days < newDays)
39            return true;
40        else
41            return false;
42    }

.aspx
 1<asp:GridView ID="gvMeiTiNews" runat="server">
 2<asp:TemplateField>
 3                <ItemStyle HorizontalAlign="Left" />
 4                <ItemTemplate>
 5                    <asp:HyperLink ID="hlnkNewsTitle" runat="server" NavigateUrl='<%# "ReadNews.aspx?NewsID=" + Eval("NewID") %>'
 6                        Text='<%# (new Common()).GetShowText(Eval("Title").ToString(),23) %>' ToolTip='<%# Eval("Title") %>'></asp:HyperLink>
 7                    <asp:Image ID="imgNew" runat="server" ImageUrl="~/theme/images/new.gif"  Visible='<%# (new Common()).IsNew(DateTime.Parse(Eval("EditDate").ToString())) %>' />
 8                    <asp:Image ID="imgHot" runat="server"  ImageUrl="~/theme/images/hot.gif" Visible='<%# (new Common()).IsHot(Int32.Parse(Eval("Clicks").ToString())) %>' />
 9                </ItemTemplate>
10            </asp:TemplateField>
11
12</asp:GridView>


2. 使用<% %>块

公共类(C#)
 1public class Common
 2{
 3    public Common()
 4    {
 5        //
 6        // TODO: 在此处添加构造函数逻辑
 7        //
 8    }

 9    //检查Count是否为0
10    public string CheckIteams(int Count)
11    {
12        if (Count <= 0)
13            return "<span>暂无</span>";
14        else
15            return string.Empty;
16    }

17}

18

.aspx
1<%  =(new Common()).CheckIteams(gvClassNewsList.Rows.Count) %>
posted @ 2008-07-13 10:46 reddyone 阅读(797) | 评论 (0)编辑 收藏
     摘要: ASP.NET 引入了新的声明性数据绑定语法。这种非常灵活的语法允许开发人员不仅可以绑定到数据源,而且可以绑定到简单属性、集合、表达式甚至是从方法调用返回的结果。下表显示了新语法的一些示例。   简单属性 Customer: <%# custID %> ...  阅读全文
posted @ 2008-07-13 10:29 reddyone 阅读(529) | 评论 (0)编辑 收藏
1. RowCommond 事件

.aspx

在GridView添加一模板列,加上CommandArgument 并绑定如下
1 <asp:GridView ID="gvAdminList" runat="server" >            
2 <asp:TemplateField>
3   <ItemTemplate>
4      <asp:LinkButton ID="lbtnLook" runat="server" CommandName="GetAdminInfo" CommandArgument='<%# Eval("AdminID") %>'>查看</asp:LinkButton>
5      </ItemTemplate>
6   </asp:TemplateField>
7  </asp:GridView>

.aspx.cs(C#)
读取CommandArgument
1protected void gvAdminList_RowCommand(object sender, GridViewCommandEventArgs e)
2{
3  if (e.CommandName == "GetAdminInfo")
4    {
5      string AdminID = e.CommandArgument.ToString();
6    }

7}

2.  RowDataBound 事件

.aspx
.......

.aspx.cs(C#)
 1protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)  
 2{      
 3  if (e.Row.RowType == DataControlRowType.DataRow)
 4  {
 5   if (e.Row.RowState == DataControlRowState.Alternate || e.Row.RowState == DataControlRowState.Normal)
 6    {
 7
 8     NEWS.NewsRow news = (NEWS.NewsRow)((System.Data.DataRowView)e.Row.DataItem).Row;
 9    }

10  }

11}








posted @ 2008-07-13 10:17 reddyone 阅读(1347) | 评论 (0)编辑 收藏
仅列出标题