TOP

[C#]動態建立控制項


重點:一定要寫在Page_Init

        protected void Page_Init(object sender, EventArgs e)
        {
            //動態新增
            DataTable dt = new DataTable();
            dt.Load(GetZipCity());

            if (dt.Rows.Count > 0)
            {
                LinkButton[] btnZipCity = new LinkButton[dt.Rows.Count];

                for (int i = 0; i < btnZipCity.Length; i++)
                {
                    btnZipCity[i] = new LinkButton();
                    btnZipCity[i].ID = "btnZipCity" + i.ToString();
                    btnZipCity[i].Text = dt.Rows[i]["CityName"].ToString();
                    btnZipCity[i].CommandName = dt.Rows[i]["CityID"].ToString();
                    btnZipCity[i].Click += new System.EventHandler(btnZipCity_Click);
                    btnZipCity[i].Command += new CommandEventHandler(btnZipCity_Click);
                    this.PlaceHolder.Controls.Add(btnZipCity[i]);

                    Label olab = new Label();
                    olab.Text = " | ";
                    PlaceHolder.Controls.Add(olab);
                }
            }
          }

        // ZipCity Button 的 Click 事件導向函數
        protected void btnZipCity_Click(object sender, EventArgs e)
        {
            LinkButton ZipCity = (LinkButton)sender;
            QueryZipCity = ZipCity.CommandName;
        }

參考資料:http://careychen.pixnet.net/blog/post/22622126-%5Basp.net%5D-%E5%9C%A8-web-form-%E5%8B%95%E6%85%8B%E5%BB%BA%E7%AB%8B%E6%8E%A7%E5%88%B6%E9%A0%85%E7%9A%84%E6%AD%A3%E7%A2%BA%E6%96%B9%E6%B3%95
TOP

[ASP.NET, jQuery] ASP.Net使用jQuery日歷選擇器

<head>
  <script>
      $(function() {          
        $("input[id*=tbStartTime]").datepicker({ showOn: "both", buttonImage: "../images/gesplus/calendar.gif", buttonImageOnly: true, dateFormat: "yy/mm/dd" });
      });
  </script>

  <style>
//調整圖片位置
    .ui-datepicker-trigger { margin-left:5px; margin-top: 5px; margin-bottom: -6px; }
  </style>
</head>


<asp:TextBox ID="tbStartTime" runat="server" Columns="10"></asp:TextBox>

參考資料:
http://blog.xuite.net/tolarku/blog/213867131-%5BAsp.net%5D+%E9%81%B8%E6%93%87%E6%97%A5%E6%9C%9F+Calendar+%E4%BD%BF%E7%94%A8+jQueryUI+%E7%9A%84+DatePicker

http://www.fbloggs.com/2009/04/16/how-to-style-the-button-image-in-jquery-datehandler-ui/

http://jqueryui.com/datepicker/#localization

http://blog.wu-boy.com/2008/04/jquery%E7%AD%86%E8%A8%98-%E5%A5%BD%E7%94%A8%E7%9A%84%E6%97%A5%E6%9C%9F%E5%87%BD%E5%BC%8F/


script取得ID的方式
一、 
$(".date-picker").datepicker()

二、

$("#<%= txtEventDate.ClientID %>").datepicker()

三、
$("input[id*=txtEventDate]").datepicker()

參考資料:
http://stackoverflow.com/questions/5115067/jquery-datepicker-not-working-in-asp-net-page

TOP

[ASP.NET]Repeater使用CheckBox的OnCheckedChanged

使用者需求:在Repeater內使用CheckBox並且勾選時立即變更資料庫狀態。
<table>
     <asp:repeater id="ContentRepeater" runat="server">
           <ItemTemplate>
                   <tr>
                        <td>
                               <asp:Literal ID="ltContentId" runat="server"
                               Text='<%# DataBinder.Eval(Container, "DataItem.ContentId") %>'>
                               </asp:Literal>
                        </td>
                       <TD>
                              <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true"                                                  OnCheckedChanged='cbCoaching_OnCheckedChanged'/>
                       </td>
                   </tr>
           </ItemTemplate>
      </asp:repeater>
</table>


    Protected Sub cbCoaching_OnCheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
         '取得在repeater內的那一欄資料。
        Dim chk As CheckBox = DirectCast(sender, CheckBox)
        Dim item As RepeaterItem = DirectCast(chk.NamingContainer, RepeaterItem)
        Dim ltContentId As Literal = DirectCast(item.FindControl("ltContentId"), Literal)
         response.write ("ltContentId =" &ltContentId  & "<BR>")
    End Sub

參考資料:
http://stackoverflow.com/questions/6997948/handling-the-checkedchanged-event-of-inner-repeater-checkbox-control-in-asp-net
http://stackoverflow.com/questions/25308247/how-to-get-the-repeater-item-in-a-checkbox-checkedchanged-event
TOP

[ASP.NET] 文字字串遇到http或https時改為超連結

Dim regx As New Regex("http(s)?://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&amp;\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?", RegexOptions.IgnoreCase)

            Dim mactches As MatchCollection = regx.Matches(MyString)

            For Each match As Match In mactches
                MyString= MyString.ToString.Replace(match.Value, "<a href='" & match.Value & "' target='_blank' >" & match.Value & "</a>")
            Next

參考文章:
http://weblogs.asp.net/farazshahkhan/regex-to-find-url-within-text-and-make-them-as-link

http://www.dotblogs.com.tw/jameswu/archive/2008/04/22/3009.aspx

http://www.allenkuo.com/EBook5/view.aspx?TreeNodeID=67&id=209

http://blog.xuite.net/xiaolian/blog/59321149-%E3%80%90C%23%E3%80%91%E5%AD%97%E4%B8%B2%E6%9C%89%E6%95%88%E8%BD%89%E6%88%90%E8%B6%85%E9%80%A3%E7%B5%90

http://smallpoint-program.blogspot.tw/2012/08/phpemail.html
TOP

[ASP.NET]網頁網址及頁籤加上Icon圖片顯示

1.製作16x16的ICON圖

2.將以下的程式碼放位<head></head>之間
方式1:
<link rel="icon" href="/favicon1.ico" type="image/x-icon" />

方式2:
<link rel="shortcut icon" href="/favicon1.ico" type="image/x-icon" />

參考資料:http://www.tshopping.com.tw/thread-164857-1-1.html
TOP

[C#] Cancel CreateUserWizard from CreatedUser Event

1.利用CreateUserWizard 建立使用者帳號,同時將其他資料儲存在自已建立的table內,不使用aspnetdb的Profiles時
2.需要在CreatedUser後儲存資料但又必需要確認是否有正確儲存的方式。
3.如果未儲存成功,刪除已註冊的使用者帳號,並且顯示錯誤訊息。

幾個重點如下:
        protected void RegisterUser_CreatedUser(object sender,  EventArgs e)
        {        
         Result = AddUserProfile(userProfile);   
         if (Result == true)
         {
           return true;
         }
         else
         {
            Membership.DeleteUser(RegisterUser.UserName);
            HttpContext.Current.Items.Add("ErrorMessage", "註冊失敗,請聯繫系統管理員!!");
            Server.Transfer(Request.Url.PathAndQuery, true);
         }
        }

       protected void RegisterUser_CreatingUser(object sender, LoginCancelEventArgs e)
        {
            if (HttpContext.Current.Items["ErrorMessage"] != null)
            {
                CustomValidator custValidator = (CustomValidator)RegisterUserWizardStep.ContentTemplateContainer.FindControl("CustomValidator1");
                custValidator.ErrorMessage = HttpContext.Current.Items["ErrorMessage"].ToString();
                custValidator.IsValid = false;
                e.Cancel = true;
            }
 
        }


<asp:CreateUserWizardStep runat="server" ID="RegisterUserWizardStep">
   <ContentTemplate>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="CustomValidator" ValidationGroup="RegisterUserWizardStep" />
   </ContentTemplate>
</asp:CreateUserWizardStep>

參考資料:http://stackoverflow.com/questions/10277812/cancel-createuserwizard-from-createduser-event