2013年2月26日 星期二

ASP.NET 彈出視窗

string myMessage = "popup me";
Page.ClientScript.RegisterStartupScript(this.GetType(), "popup", "alert(' " +myMessage+ " ');", true);

2013年2月25日 星期一

Repeater 綁定資料

in code-behind:
string cnString = System.Configuration.ConfigurationManager.ConnectionStrings["cnString"].ConnectionString;
string queryString = "select * from employees";
SqlConnection con = new SqlConnection(cnString);
SqlDataAdapter adapter = new SqlDataAdapter(queryString, con);

DataSet ds = new DataSet();
adapter.Fill(ds, "employees");
Repeater1.DataSource = ds.Tables["employees"];
Repeater1.DataBind();

default.aspx:
<asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate>
        <table>
           <tr>
               <th>ID</th>
               <th>Last Name</th>
               <th>First Name</th>
               <th>Hired Date</th>
           </tr>
</HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td><%# Eval("employeeid") %></td>
            <td><%# Eval("lastname") %></td>
            <td><%# Eval("firstname") %></td>
            <td><%# Eval("hiredate") %></td>
        </tr>
   </ItemTemplate>

   <FooterTemplate>
       </table>
   </FooterTemplate>
</asp:Repeater>

在 web.config 設定連線字串

in web.config:
<configuration>
    <connectionStrings>
        <add name="cnString" connectionString="myConnectionString">
    </connectionStrings>
</configuration>
in code-behind:
using System.Configuration;
string connectString = ConfigurationManager.ConnectionStrings["cnString"].ConnectionString;

2013年2月23日 星期六

取得 Repeater 裡的控制項

假設一個 Repeater 裡包含一個超連結:
<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <asp:HyperLink ID="linkInRepeater" runat="server">link</asp:HyperLink>
    </ItemTemplate>
</asp:Repeater>

要取得 HyperLink 控制項,需添加 OnItemDataBound 屬性給 Repeater :
<asp:repeater ID="Repeater1" runat="server" OnItemDataBound="getControl" >

接著在 .cs 裡設定 getControl :
protected void getControl(object sender, RepeaterItemEventArgs e) {
    HyperLink myLink = (HyperLink)e.Item.FindControl("linkInRepeater");
    myLink.Text = "change text"; // 變更連結文字
}

2013年2月19日 星期二

CalendarExtender with BoundField

假設在GridView中有個輸入欄位,格式為日期:
<asp:BoundField DataField="hireDate" HeaderText="Date Hired" />

2013年2月17日 星期日

C# Double Output Digit

double number = 1.83333;
label1.Text = sum.ToString("0.##");
result:
1.83

C# 產生亂數

Random rnd1 = new Random();
int month = rnd1.Next(1, 13);    // 產生介於 1~12 的亂數
int number = rnd1.Next(100);    // 產生介於 0~99 的亂數

2013年2月11日 星期一

在 MasterPage 中使用 PreviousPage.FindControl

假設 source.aspx 包含兩個控制項,例如:
<asp:TextBox ID="myBox" runat="server" />
<asp:Button ID="btn" runat="server" Text="click me" onclick="btn_Click" />
in source.aspx.cs:
protected void btn_Click(object sender, EventArgs e) {
    Server.Transfer("target.aspx");
}

目標頁面 target.aspx :

<asp:Label ID="myLabel" runat="server" Text="default_text" />

接著想在 target.aspx 中使用 PreviousPage.FindControl 取得 source.aspx 的 TextBox 值,則要加入:

<%@ PreviousPageType VirtualPath="~/source.aspx" %>

then in target.aspx.cs:

if(Page.PreviousPage != null){
    var textbox = (TextBox) Page.Previous.FindControl("myBox");
    Label1.Text = textbox.Text;
}

如此則按下 source.aspx 的 Button 後,會在 target.aspx 中的 Label 顯示 Input 欄位的值。
但如果頁面是 MasterPage 裡的 ContentPage,則失效 (取到 null )。


解法:

透過 ContentPlaceHolderID,先找到 Content,再取控制項。

上面的 target.aspx.cs 改為:
if(Page.PreviousPage != null){
   var preContent =  (ContentPlaceHolder)
                   Page.PreviousPage.Master.FindControl("myContentPlaceHolderID");
    TextBox textbox = (TextBox) preContent.FindControl("myBox");
    Label1.Text = textbox.Text;
}

參考: http://www.aspnet101.com/2008/01/using-previouspage-with-a-master-page/










2013年2月4日 星期一

ASP.NET 取得 Master Page 的控制項

指定文字:

in MasterPage:
<asp:Label id="label1" runat="server"/>
<input type="text" id="textbox1" runat="server"/>

in ContentPage.:
Label myLabel = (Label)Master.FindControl("label1");
myLabel.Text = "get ASP Label";
HtmlInputControl myBox = (HtmlInputControl)Master.FindControl("textbox1")
myBox.Value = "get Html TextBox";


指定class:

in MasterPage:
<ul id="List" runat="server">
    <li id="listItem1" runat="server"><a href="#">myLink</a></li>
</ul>

in ContentPage:
HtmlGenericControl myList = (HtmlGenericControl)Master.FindControl("listItem1");
myList.Attributes.Add("class", "currentClassName");

指定單一屬性:
myList.Style.Add("background-color", "currentColor");