2012年11月30日 星期五

更改DataTable欄位名稱


DataBase如果回的資料是DataSet或是DataTable
而欄位資料與要Bind的的GridView不同
如果又不想要更改Bind這邊的欄位名稱
則可以直接更改DataTable的Cloumns


o_dt = Procedures.GetDataSet.Tables[0];
DataTable dt = o_dt;
dt.Columns["ACCT_TYPE_ID"].ColumnName = "ACCT_DATA";
dt.Columns["HIDACCT_TYPE_ID"].ColumnName = "ACCT_DATA2";

2012年11月15日 星期四

調整視窗大小 asp.net



    function resizeWindow()
{
//    var width = screen.availWidth;  // 螢幕可用寬度
//    var height = screen.availHeight;    // 螢幕可用長度
//    alert('最大化視窗大小:' + width + ' * ' + height);
//    moveTo(0,0);
//    resizeTo(width, height);
//  
 


2012年11月14日 星期三

textbox 提示效果


VB.NET

textbox.Attributes.Add("Value", "請輸入帳號")
textbox.Attributes.Add("OnFocus", "if(this.value=='請輸入帳號') {this.value=''}")
textbox.Attributes.Add("OnBlur", "if(this.value==''){this.value='請輸入帳號'}")


javascript

Attributes Even

OnFocus="javascript:if(this.value=='請輸入帳號') {this.value=''}"

OnBlur="javascript:if(this.value=='') {this.value='請輸入帳號'}"

----------------------------------------------------------------------------


OnFocus="javascript:if(this.value=='請輸入帳號') {this.value=''}"

OnBlur="javascript:if(this.value==''){this.value='請輸入帳號'}">


2012年11月12日 星期一

iframe SRC 使用JavaScript

window.frames['iframeView'].location.href=document.getElementById('TextBox').value

asp.net ajax 彈不出alert對話框的解決辦法

vb 寫法ScriptManager.RegisterStartupScript(Button2, GetType(Button), "JavaScript_Sample1", "window.alert(""Hello!! Code Behind!!"")", True)
c# 寫法ScriptManager.RegisterClientScriptBlock(this.Page, typeof(Page), "message", "javascript:alert('" + "YouAre Msg" + "')", true);

-------------------------------------------

asp.net ajax 彈不出alert對話框的解決辦法

想用js腳本輸出一些提示,可是不管是用 Page.RegisterClientScriptBlock還是用 Page.RegisterStartupScript

注冊,該死的提示信息總是不出來。其實不是不出來,只是我們叫錯了它們的名字,我們只要用下面的這條語句︰

 ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", "alert('提示信息')", true);

就可以正常的彈出alert對話框了。

    注︰如果在頁面中有多個UpdatePanel,如果每個UpdatePanel的UpdateMode都是“always”[默認值]的話,

那麼你使用那個UpdatePanel的實例作為參數都可以;如果每個UpdaePanel的UpdateMode=conditional,

那麼你必須使用正在更新的那個UpdatePanel作為參數,這樣腳本才能起作用。


所以...我把msg  Function 改為..
        ///

        /// display msg : msg("")
        ///
        ///
        protected void AJAX_msg(string msgstr)
        {
            msgstr = msgstr.Replace("'", "").Replace("\n", "").Replace("\r", "").Replace('"', '”').Replace("(", "(").Replace(")", ")").Replace(";", ";");
            string javastr  = "alert('" + msgstr.ToString() + "' );\n";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "AJAX_msg", javastr, true);
        }


///轉載 網路上

ScriptManager.RegisterClientScriptBlock(this.Page, typeof(Page), "message", "javascript:alert('" + GetMsg.GetCode("W0834") + "')", true);

ScriptManager.RegisterClientScriptBlock(this.Page, typeof(Page), "message", "javascript:alert('" + "YouAre Msg" + "')", true);

---------------------------------------

protected void Page_Load(object sender, EventArgs e)
    {
     
        string js="document.getElementById('btn').value='測試';";
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), Guid.NewGuid().ToString(), js, true);
    }
js註冊在
底下,button上面,這時候button還沒載入,會出現js錯誤
如果您在Code-Behind寫了
    protected void Page_Load(object sender, EventArgs e)
    {
         string js="document.getElementById('btn').value='測試';";
        ScriptManager.RegisterStartupScript(this,this.GetType(),Guid.NewGuid().ToString(),js,true);
    }
js註冊在button之後,
前面,因為這時候button已經載入,js的程式碼抓得到button目標,執行成功
如果是單純做頁面跳頁(window.location.href)
我都是用
RegisterClientScriptBlock,這樣使用者還沒看到畫面,頁面就已經跳頁
如果要抓取頁面上的DOM做處理,我都是用RegisterStartupScript 確保js一定抓得到頁面上的DOM

2012年11月2日 星期五

RadioButton 使用javascript 在Gridview 中單選





//GridView OnRowDataBound
protected void grid_view_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                RadioButton rb = (RadioButton)e.Row.FindControl("RadioButton1");
                rb.Attributes.Add("onclick", "judge(this)");//給RadioButton加onclick屬性
            }
        }

Dialog 開啟新視窗傳值


Step.1 page_load in code

this.Button1.Attributes.Add("OnClick", "return getRelPeople('" + this.returndata.ClientID.ToString() + "','" + this.Session["LoginID"].ToString() + "','" + this.Session["CASE_CUSTSYSID"].ToString() + "');");

Step.2 js  

function getRelPeople(Target, LoginID, CUST_SYSID) {
    var width = screen.availWidth;
    var height = screen.availHeight - 50;
    var returnValue = window.showModalDialog("CASE_BROW_GET_XXXXX.aspx?LoginID=" + LoginID + "&CUST_SYSID=" + CUST_SYSID + "&wType=Case&TFB_Table=CFW_ORDER_TABLE", 'TradeList', "scrollbars=yes;location=no;resizable=yes;help=no;status=no;dialogHeight:" + height + "px;dialogwidth:" + width + "px;");
    if (returnValue != null) {
        window.document.getElementById(Target).value = returnValue;
        return true; //&ChkFlg=Y
    }
    return false;
}

Step.3 CASE_BROW_GET_XXXXX.aspx
.....
.....
.....
string returndata = this.ViewState["GETCUST"].ToString();
ScriptManager.RegisterClientScriptBlock(this.Page, typeof(Page), "message", "javascript:sent2('" + returndata + "');window.close();", true);
return;

Step.4 js    

function sent2(seletedValue) {
    window.returnValue = seletedValue;
}

GridView合併欄位


//GridView合併欄位 GridView加入OnPreRender事件
i =Rows ;j = Columns; Cells =

protected void grid_view_PreRender(object sender, EventArgs e)
        {
            for (int j = 2; j < 3; j++)
            {
                int i = 1;
                foreach (GridViewRow wkItem in grid_view.Rows)
                {
                    if (Convert.ToInt32(wkItem.RowIndex) == 0)
                    {
                        wkItem.Cells[j].RowSpan = 1;
                        wkItem.Cells[j+1].RowSpan = 1;
                    }
                    else
                    {
                        //if (wkItem.Cells[j].Text.Trim() == grid_view.Rows[Convert.ToInt32(wkItem.RowIndex) - i].Cells[j].Text.Trim())
                        if (((Label)wkItem.FindControl("lblGUAR_NM")).Text.Trim() == ((Label)grid_view.Rows[Convert.ToInt32(wkItem.RowIndex) - i].FindControl("lblGUAR_NM")).Text.Trim())
                        {
                            grid_view.Rows[Convert.ToInt32(wkItem.RowIndex) - i].Cells[j].RowSpan += 1;
                            grid_view.Rows[Convert.ToInt32(wkItem.RowIndex) - i].Cells[j+1].RowSpan += 1;
                            i = i + 1;
                            wkItem.Cells[j].Visible = false;
                            wkItem.Cells[j+1].Visible = false;
                        }
                        else
                        {
                            grid_view.Rows[Convert.ToInt32(wkItem.RowIndex)].Cells[j].RowSpan = 1;
                            grid_view.Rows[Convert.ToInt32(wkItem.RowIndex)].Cells[j+1].RowSpan = 1;
                            i = 1;
                        }
                    }
                }
            }
        }

GridView RaidoButon Group 不複選


//RadioButton加入事件
 OnCheckedChanged="RadioButton_CheckedChanged"  AutoPostBack="true"


//RadioButton 事件CheckedChanged
protected void RadioButton_CheckedChanged(object sender, EventArgs e)
        {
            string RadioNowGUAR_SYSID = ((Label)((RadioButton)sender).NamingContainer.FindControl("lblGUAR_SYSID")).Text; //抓取當下RadioButton
            string RadioNowACCT_DATA = ((Label)((RadioButton)sender).NamingContainer.FindControl("lblACCT_DATA")).Text; //抓取當下RadioButton
            string tempGUAR_SYSID,tempACCT_DATA;
            foreach (GridViewRow item in grid_view.Rows)  //此為檢查是否有相同Group
            {
                if (((RadioButton)item.FindControl("RadioButton2")).Checked == true) //此為檢查是否為同Group
                {
                    tempGUAR_SYSID = ((Label)item.FindControl("lblGUAR_SYSID")).Text;
                    tempACCT_DATA = ((Label)item.FindControl("lblACCT_DATA")).Text;
                    if (tempGUAR_SYSID == RadioNowGUAR_SYSID)
                    {
                        if (tempACCT_DATA != RadioNowACCT_DATA)
                        {
                            ((RadioButton)item.FindControl("RadioButton2")).Checked = false;
                        }
                    }
                }
            }
           
        }

字串轉list跑foreach


//字串帶入
List RadioListB = RadioStrB.Split(',').ToList();
         foreach (string item in RadioListB)
         {
            if (item != "")
            {
                ((RadioButton)grid_view.Rows[Convert.ToInt16(item)].FindControl("RadioButton2")).Checked = true;  
               }
               
           }

以ViewState建立GridView


以ViewState建立Table 並建立新欄位


  if (this.ViewState["tempCUST"] == null)
            {
                DataTable tempdt = new DataTable();
                tempdt.Columns.Add("BORROWER_NM");
                tempdt.Columns.Add("GUAR_NM");
                tempdt.Columns.Add("ACCT_DATA");
                tempdt.Columns.Add("ACCT_DATA2");
                tempdt.Columns.Add("GUAR_SYSID");
                //tempdt.Rows.Add(tempdt.NewRow());
                this.ViewState["tempCUST"] = tempdt;
            }




string returnValue = this.returndata.Value;   //某個Table轉字串接過來
string[] arrreturnValue = returnValue.Split(';'); //分割符號



  DataTable dt = (DataTable)ViewState["tempCUST"];

            foreach (string item in arrreturnValue)
            {
                if (!string.IsNullOrEmpty(item))
                {
                    string[] arrItem = item.Split(',');
                    string strBORROWER_NM = arrItem[0];
                    string strGUAR_NM = arrItem[1];
                    string strACCT_DATA = arrItem[2];
                    string strACCT_DATA2 = arrItem[3];
                    string strGUAR_SYSID = arrItem[4];


                    DataColumn dc = new DataColumn();
                    DataRow dr = dt.NewRow();
                    dr["BORROWER_NM"] = strBORROWER_NM;
                    dr["GUAR_NM"] = strGUAR_NM;
                    dr["ACCT_DATA"] = strACCT_DATA;
                    dr["ACCT_DATA2"] = strACCT_DATA2;

                    dr["GUAR_SYSID"] = strGUAR_SYSID;
                    dt.Rows.Add(dr);

                }
            }
            ViewState["tempCUST"] = dt;
            this.grid_CustW03.DataSource = dt;
            this.grid_CustW03.DataBind();


GridView 塞空值


 DataTable dt = new DataTable();


 dt.Rows.Add(dt.NewRow());
 dt.Rows.Add(dt.NewRow());
 dt.Rows.Add(dt.NewRow());

 this.grid_CustW03.DataSource = dt;
 this.grid_CustW03.DataBind();

FindControl

//一般 FindControl RadioButton為例
((RadioButton)grid_view.Rows[i].FindControl("RadioButton2")).Checked == false




 foreach (GridViewRow item in grid_view.Rows)
{

    if (((RadioButton)item.FindControl("RadioButton2")).Checked == true)
            {
             ....
            }

}


///指定當下選擇的物件

 protected void RadioButton_CheckedChanged(object sender, EventArgs e)
 {


            string RadioNowGUAR_SYSID = ((Label)((RadioButton)sender).NamingContainer.FindControl("lblGUAR_SYSID")).Text;
....
}