Analytics

顯示具有 Attributes 標籤的文章。 顯示所有文章
顯示具有 Attributes 標籤的文章。 顯示所有文章

2013年11月25日 星期一

[ASP.NET]使用 ListView.ItemTemplate將ListView裡將某一行置換顏色 (Use ListView.ItemTemplate the ListView will be replaced in a row colors)


問題
使用 ListView.ItemTemplate將ListView裡將某一行置換顏色
在aspx註記tr為runat="server"並且賦予一個id,當listview的資料符合置換顏色的條件時,藉以取得該行(tr裡的所有td)變更顏色





解決方法
.aspx
<asp:ListView ID="ListView1" runat="server" EnableModelValidation="True"

OnItemDataBound="ListView1_ItemDataBound">

<LayoutTemplate>
    <table id="Table1" runat="server">
        <tr>
            <td>
                 <table id="itemPlaceholderContainer" runat="server" border="1" style="margin-left: 0px; border-collapse: collapse; border-color: #696969;">
                         <tr style="color:#FFFFFF;background-color:#0000FF">
                            <th>Product</th>
                            <th>Material</th>
                        </tr>
                </table>
             </td>
        </tr>
</LayoutTemplate>
<ItemTemplate>
    <tr style="background-color:#FFFFCC" id="trRow" runat="server">
        <td>
            <asp:Label ID="Label1" runat="server" Text='<%# Eval("Product") %>' />
        </td>
        <td>
             <asp:Label ID="lblMaterial" runat="server" Text='<%# Eval("Material") %>' />
                </td>
    </tr>
</ItemTemplate>
</asp:ListView>

.cs
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    string type = ((Label)e.Item.FindControl("lblMaterial")).Text;

    if (type == "SUM")
    {
        var dataItem = e.Item as ListViewDataItem;
        if (dataItem != null)
        {
            var innerTR = dataItem.FindControl("trRow");
            if (innerTR != null)
            {
                ((HtmlTableRow)innerTR).Attributes["style"] = "background-color: #C0C0C0";
            }
        }
    }
}

2012年6月27日 星期三

[ASP.NET]使用 Javascript做出Foucs時,TextBox全選()When you use Javascript to make Focus, TextBox Select


問題
使用 Javascript做出Foucs時,TextBox全選



解決方法
public void SetTextBoxSelected(TextBox from)
{
    from.Attributes.Add("onfocus", "document.getElementById('" + from.ClientID + "').select();");
}
or
public void SetTextBoxSelected(TextBox from)
{
   from.Attributes.Add("onfocus", "javascript:this.select();");
}

2012年6月20日 星期三

[ASP.NET]使用 Javascript做出按下Button顯示對話視窗(Press the Button to make use Javascript to display a dialog window)


問題
使用 Javascript做出按下Button顯示對話視窗



解決方法
/// <summary>
/// 按鈕需確認時繫節
/// </summary>
/// <param name="msg"></param>
/// <param name="bt"></param>
public void AddOnClickConfirm(string msg, Button bt)
{
    bt.Attributes.Add("OnClick", string.Format("javascript:if(confirm('{0}')==false) return false; ", msg));
}

[ASP.NET]使用 Javascript做出按下Enter時,focus到指定Button(When you use Javascript to make press Enter, focus to the specified Button)


問題
使用 Javascript做出按下Enter時,focus到指定Button



解決方法
/// <summary>
/// 畫面上按下ENTER時,FOCUS到指定的BUTTON位置
/// </summary>
/// <param name="from"></param>
/// <param name="to"></param>
public void SetTextBoxFocusTo(TextBox from, Button to)
{
    from.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + to.ClientID + "').click();return false;}} else {return true}; ");
}

[ASP.NET]使用 Javascript做出按下Enter時,focus到指定TextBox(When you use Javascript to make press Enter, focus to the specified TextBox)


問題
使用 Javascript做出按下Enter時,focus到指定TextBox



解決方法
/// <summary>
/// 畫面上按下ENTER時,FOCUS到指定的TEXTBOX位置
/// </summary>
/// <param name="from"></param>
/// <param name="to"></param>
public void SetTextBoxFocusTo(TextBox from, TextBox to)
{
    from.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + to.ClientID + "').focus();return false;}} else {return true}; ");
}

2012年6月2日 星期六

[ASP.NET]使用 TextBox.Attributes當Focus時,TextBox內容全選(When using TextBox.Attributes when Focus, TextBox or select all)


問題
使用 TextBox.Attributes當Focus時,TextBox內容全選



解決方法
在PageLoad事件裡加入
Textbox1.Attributes.Add("onfocus", "SetSelected();")
在aspx上加入
function SetSelected()
{
document.getElementById("Textbox1").select();
}
或是直接在cs裡指定
Textbox1.Attributes.Add("onfocus", "javascript:this.select();")

2010年8月6日 星期五

[ASP.NET]解決 TextBox控制項ReadOnly後無法取消ReadOnly (After resolving not cancel ReadOnly ReadOnly TextBox controls)


問題

TextBox控制項ReadOnly後無法取消ReadOnly

在TextBox控制項設為ReadOnly時,當Postback後,即使屬性改為
ReadOnly為False,一樣只能唯讀??

解決方法

改用移除繫結屬性方式
Me.txtTotalNum.Attributes.Remove("readonly")

2010年8月3日 星期二

[ASP.NET]解決 TextBox控制項ReadOnly時造成Postback後值被清空的問題(After solving causing Postback value is cleared when the TextBox controls ReadOnly problem)


問題
TextBox控制項ReadOnly時造成Postback後值被清空的問題
在TextBox控制項設為ReadOnly時,會發生觸發Server端事件後,TextBox的值不見了!




解決方法
改用繫結屬性方式

Me.txtTotalNum.Attributes.Add("readonly", "true")

熱門文章