c#常用编程方法 (一)

1.将字符串NoList以','作为标记转换为字符串数组,用string[] arrList=NoList.Split(',')
2.关掉打开的当前窗口:
public static void CloseWindow(Page page)
{
  string strScript="<script language=/"JavaScript/" type=/"text/javascript/">/n"+
                    "window.close();/n"+
   "</script>/n";
 page.Response.Write(strScript);
}
3.判断字符串是否为空:string.IsNullOrEmpty(this.str);
4.ViewState用法
 if(ViewState["OperationType"]!=null)
 {
  return (string)ViewState["OperationType"];
 }
5.this.Page.RegisterStartupScript("note","<script LANGUAGE = JScript>window.alert('保存成功!'); window.document.URL='Supervise.aspx';</script>");
6.Session能够进行页面之间的传值
 如test1.aspx的页面类中付值Session["DirectoryName"] = aa[0].ToString();当页面有test1.aspx跳转到test2.aspx后在test2.cs中可以通过这样的方法取值
 if(Session[DirectoryName]!=Null)
 {
  string temp=Session[DirectoryName];
 }
7.另外一种页面间传值的方法是在test1.cs中 this.Response.Redirect("test2.aspx?id=6");
 然后再test2.cs中通过这样的方法来取得该id
if (this.Request.QueryString["id"] == null)
 {
   string ID=this.Request.QueryString["AutoNo"].ToString().Trim();
}6,7两种方法在编程中会经常用到的
8. 在同一个页面之间不同函数间传值时ViewState的用处也很大
  付值:ViewState["OperationType"]=1;
  调用:
   if (ViewState["OperationType"] != null)
      {
         return Convert.ToString(ViewState["OperationType"]);
      }
9.对于用户从界面输入的信息,要注意进行字符校验,然后进行程序处理
一般采用这种方式
if (!this.CheckInput())
        {
           //处理过程
        }
10.类型转换:int.Parse  Convert.ToString()  Convert.ToDateTime等等
11.编写程序时注意随时在不太好理解的地方加上注释,对一些常用的函数要做成公用的函数以便调用
12.为界面按钮添加事件
 this.BtnDel.Attributes.Add("onclick","return confirm('确认删除?')");
13.有时候希望数据直接绑定到调用时定义的表dtTable,则采用out来进行数据回传,调用函数:GetDataTableFromID(id,out dtTable)
   被调用函数:GetDataTableFromID(int id, out DataTable drReturn)
14.设置字符串形式如果data1为string型,data2位int型,data3为DateTime型,则按如下方式来写
string.Format("输出的三个数字为:'{0}',{1},'{2}'",data1,data2,data3),后面的数据data1,data2,data3将会代替{}中的内容输出。
15.设置一个实体类的属性:
 private int m_intAutoID;
public int AutoID
{
  get
 {
   return m_intAutoID;
 }
  set
 {
  m_intAutoID=value;
 }
}
16.ArrayList的使用方法:
ArrayList 是一种动态数组,下面给出一个简单的例子:
ArrayList List=new ArrayList();
for(int i=0;i<10;i++)
List.Add(i);
List.RemoveAt(5);
for(int i=0;i<3;i++)
List.Add(i+20);
Int32[] values=(Int32[])List.ToArray(typeof(Int32));
17.GridView中队LinkButton模版列的操作:
前台界面
<asp:TemplateColumn HeaderText="操作">
 <HeaderStyle HorizontalAlign="Center" Width="15%"></HeaderStyle>
 <ItemStyle HorizontalAlign="Center"></ItemStyle>
  <ItemTemplate>
   <asp:LinkButton id="lnbView" CommandName="View" CausesValidation="False" Runat="server">查看</asp:LinkButton>
   <asp:LinkButton id="lnbErase" CommandName="Erase" CausesValidation="False" Runat="server">删除</asp:LinkButton>
  </ItemTemplate>
 </asp:TemplateColumn>
后台代码的处理部分
protected void drgDisplay_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "View":
                //处理部分
                break;
            case "Erase":
                //处理部分
                break;
        }
    }
18.获得一个随机数:
   string temp;
   temp=new Random().Next(10000,99999).ToString();
19.在html界面定义一个javascript函数,然后在服务器端调用该函数
  html界面:
  <script type="text/javascript" lanaguage="javascript">
  function cwin(strDepart)
 {
  var a=strDepart;
  var mikecatstr=window.ShowModalDialog('../../UserControl/UcSelectPerson.aspx',a,'dialogHeight:500px;dialogWidth:585px');
  //处理部分
 }
 后台调用代码:
 string a="343";
 this.ImaBu.Attributes.Add("onclick","cwin("+a+")");
20.一般在后台弹出提示框的写法: Response.write(<script>alert("请重先输入密码!")</script>)
   也可以专门做一个提示函数放到公用方法中,然后在前台进行调用就可以了
   函数:  (假设这个函数在Share类中)
   public static void PromptMessage(Page page,string strMessage)
  {
   StringBuilder sb=new StringBuilder("<script language='javascript'> window.");
   sb.Append("alert('");
   sb.Append(StringUtility.HtmlMsgStringFormatFix(strMessage));
   sb.Append("')");
   sb.Append("</script>");
   if(!page.ClientScript.IsClientScriptBlockRegistered("display"))
 {
  page.ClientScript.RegisterStartupScript(page.GetType(),"display",sb.ToString());
}
  }
后台程序调用
 Share.PromptMessage(this.Page, "联系人手机号不能为空!");
21. 用javascript脚本输出界面的一个单元格,其中有小时,分钟的选择的两个下拉选择框
tableDate = mainIFrame.document.createElement("TABLE");
_TR = tableDate.insertRow();
 _TD = _TR.insertCell(); 
 _TD.height = 1;
 _TD.bgColor = "black";
 _TR = tableDate.insertRow();
 _TD = _TR.insertCell();  
 _TD.innerHTML="<table cellspacing=0 cellpadding=0 class=calendar style='border:0px solid;width:100%'>"
                 +"<tr><td width=60 align=center>时间:</td><td><select id=/"drophour/" οnchange=/"return parent.ChangeHour(this);/" ><option>0</option><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option><option>6</option><option>7</option><option>8</option><option>9</option><option>10</option><option>11</option><option>12</option><option>13</option><option>14</option><option>15</option><option>16</option><option>17</option><option>18</option><option>19</option><option>20</option><option>21</option><option>22</option><option>23</option></select></td>"
                 +"<td>点</td>"
                 +"<td><select id=/"drophour/" οnchange=/"return parent.ChangeMinute(this);/"><option>0</option><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option><option>6</option><option>7</option><option>8</option><option>9</option><option>10</option><option>11</option><option>12</option><option>13</option><option>14</option><option>15</option><option>16</option><option>17</option><option>18</option><option>19</option><option>20</option><option>21</option><option>22</option><option>23</option><option>24</option><option>25</option><option>26</option><option>27</option><option>28</option><option>29</option><option>30</option><option>31</option><option>32</option><option>33</option><option>34</option><option>35</option><option>36</option><option>37</option><option>38</option><option>39</option><option>40</option><option>41</option><option>42</option><option>43</option><option>44</option><option>45</option><option>46</option><option>47</option><option>48</option><option>49</option><option>50</option><option>51</option><option>52</option><option>53</option><option>54</option><option>55</option><option>56</option><option>57</option><option>58</option><option>59</option></select></td>"
                 +"<td width='150' align=left>分</td>"
                 +"</tr></table>";
22.在服务器后台写script脚本,打开一个新页面
   Share.OpenWindow(this,"ExportToExcel.aspx?DepartmentNo="+DepartNo)
   在Share中的OpenWindow函数:
    public static void OpenWindow(Page page,string strUrl)
{
 string strScript;
 strScript="<script language=javascript>";
strScript+="var intHeight=600;";
strScript+="var intWidth=window.screen.width-150;";
strScript+="var strFeature='height='+intHeight+',width='+intWidth+',left=80,toolbar=no,status=yes,menubar=yes,location=yes,resizable=yes,scrollbars=yes';";
strUrl="window.open('"+strUrl+"','_blank',strFeature,true);";
strScript+=strUrl;
strScript+="</script>";
 if (!page.ClientScript.IsStartupScriptRegistered("windowOpen"))
            page.ClientScript.RegisterStartupScript(page.GetType(), "windowOpen", strScript);
}
23.GridView的数据绑定
 前台html:
  <asp:TemplateField HeaderText="标 题">
                    <ItemTemplate>
                        <asp:HyperLink ID="HyperLinkTitle" runat="server" Text='<%# Eval("Title") %>' CssClass="link3"></asp:HyperLink>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Left" Wrap="False" />
                    <HeaderStyle HorizontalAlign="Center" Wrap="False" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="时 间">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("AddTime","{0:yyyy-MM-dd HH:mm}") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" Width="120px" Wrap="False" />
                    <HeaderStyle HorizontalAlign="Center" Width="120px" Wrap="False" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="信息类别">
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# htTypeId[Eval("HotLineTypeID")] %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" Width="100px" Wrap="False" />
                    <HeaderStyle HorizontalAlign="Center" Width="100px" Wrap="False" />
                </asp:TemplateField>
对于Text='<%# Eval("Title")这种格式,当指定GridView绑定的数据源后,会自动在界面上绑定出Title字段的数据
对于Text='<%# htTypeId[Eval("HotLineTypeID")] %>'则需要在后台程序中添加这样的程序
public Hashtable htTypeId
    {

        get
        {

            if (ViewState["htTypeId"] != null)
            {

                return (Hashtable)ViewState["htTypeId"];
            }
            else
            {

                return null;
            }
        }
        set
        {

            ViewState["htTypeId"] = value;
        }
    }
将返回的值进行绑定
24.关于用户控件
   在页面中重复出现较多的模块,我们可以把它做成用户控件,比如网站的头尾模块。这样我们在每个页面需要添加头尾模块时只需要
 将这个控件拖入网页中即可,用户控件以.ascx结尾。
25.自定义控件,对于一些功能模块,如果使用频繁,我们也可以把它做成一个控件,比如日期控件(在论坛上已提供下载)需要使用时直接从面板中拖入即可,一般是建一个类库,
调试完成后,编译成.dll文件,在工具箱中点击选择项,将该dll文件添加进取,就可以像普通TextBox一样去使用它了。
26.前台用脚本编写dropDownList的onchange事件,后台编写控件的触发脚本
 drpType.Attributes.Add("onchange",changeType();");
 前台:
 <script language="javascript">
 function changeType()
{
  if(document.all.drpYwlb.value!="")
{
  if(document.all.drpYwlb.value!="Select")
{
 eval("document.all.querycondition"+document.all.drpYwlb.value+".style.display=''")
}
}
 else
{
 for(iii=1;iii<5;iii++)
 {
  eval("document.all.querycondition"+iii+".style.display='none'");
 }
}
}
27.GridView中如何对其所绑定的模版列数据进行操作
 前台:<asp:TemplateField HeaderText="序号">
               <ItemStyle HorizontalAlign="Center" Wrap="False" Width="40px" />
                    <ItemTemplate>
                       <asp:Label ID="lblIndex" runat="server">Label</asp:Label>
                     </ItemTemplate>
                <HeaderStyle Width="40px" />
        </asp:TemplateField>
咱们在后台对lblIndex这个Label进行处理
protected void gvPeople_RowDataBound(object sender,GridViewRowEventArg e)
{
 if(e.Row.RowType==DataControlRowType.DataRow)
 {
  Label lblIndex;
  lblIndex=(Label)e.Row.Cell[1].FindControl("lblIndex");
  lblIndex.Text=Convert.ToString(2);
 }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值