Friday, 12 February 2016

Some Asp.net code snippets

TextChange event in ASP.NET (type in one textbox and get in other control instantly)
CodeBehind
txtPrice.Attributes.Add("onkeyup", "rewriteLabel()");
txtRtNon.Attributes.Add("onkeyup", "rewriteLabelNon()");

HTML side
function rewriteLabel()
{
       document.getElementById('txtCFPrice').value = document.getElementById('txtPrice').value;
}

function rewriteLabelNon()
 {
       document.getElementById('txtCFNon').value = document.getElementById('txtRtNon').value;
}
Clear the columns of GridView
grdview.Columns.Clear()

Attach Scrollbar in a GridView
<DIV style="height:400px; overflow:auto;Width:775px">
GridView here
</DIV>
String cookiename = TextBox1.Text;
        //grab cookie
        HttpCookie cookie = Request.Cookies[cookiename];
        //exists?
        if (null == cookie)
        {
            Label1.Text = "cookie not found";
        }
        else
        {
            password.Text=cookie.Value.ToString();
        }
Solution
TextBox2.Attributes.Add("value", cookie.Value.ToString());
Description
When the TextMode property of an ASP.NET TextBox is set to Password the value set in the Text property will not display at runtime. This can be a pain, however it is actually by design to prevent the unmasked password from being displayed in the HTML source of the page.
While the security reasons are good to not display the masked password value, leaving it unmasked in the source, it is also necessary at times to display the masked value in the TextBox. For example, a user profile page where the user has the ability to change their password. It makes sense to display it there. After all, the user has already authenticated to get to the page (although the value is sent with the data to the browser and could easily be sniffed).
Security reasons aside, you can work around this by adding the password value to the control as as Attribute. Since the TextBox renders as an HTML input control, you can set the value attribute easily, just as you would set the Text property.

Use this to set the value, instead of setting the Text property. You can still read the value from the control via the Text property.

If you get a error like multiple item can be selected for the dropdownlist.
How to assign text/value to a dropdownlist
The first method to select an item in an ASP.NET DropDownList control is using SelectedIndex property. To find the SelectedIndex of an item, you can use IndexOf method and pass use FindByValue method. Here is the code snippet to select item Mahesh" in the DropDownList.
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue("Mahesh"))
Alternatively, you can loop through all items of the DropDownList and compare the value of the item and use Selected property to select the matched item.
Private Function SelectCurrentQuarterAndYear(ByVal stringToSelect As String)
        DropDownList1.ClearSelection()
        For Each item As ListItem In DropDownList1.Items
            If item.Text = stringToSelect Then
                item.Selected = True
                Exit For
            End If

       
Next
End Function
To Disable Browser’s back button

Just put in html side of every page (from which page , you are coming ang to the page you came. Then only you can stop the back button for both the pages. If you are using common user control in all pages the in user contro’s html side put this code.)

<script language="javascript">
        window.history.forward(1);
</script>
MessageBox with alert in DOTNET
protected void ShowMessageBox(string messageDisplay)
        {
            string sJavaScript = "<script language=javascript>\n";
            sJavaScript += "alert('" + messageDisplay + "');\n";
            sJavaScript += "</script>";
            Page page = HttpContext.Current.Handler as Page;

            if (page != null)
            {
                messageDisplay = messageDisplay.Replace("'", "\'");
                ScriptManager.RegisterStartupScript(page, page.GetType(), "err_msg", "alert('" + messageDisplay + "');", true);

            }
        }
You can not add HTML i
nside JavaScript alert(). You can do some custom popup for this porpose like on as jQuery Dialog

No comments:

Post a Comment

Baisic Useful Git Commands

  Pushing a fresh repository Create a fresh repository(Any cloud repository). Open terminal (for mac ) and command (windows) and type the be...