Get Extension from Path/URL
Most often, we will get requirement to
extract extension of a link in C#. The below code does that easily.
string ext =
System.IO.Path.GetExtension(@"F:\Articles\ASP.Net Tips\tips.doc");
or
string ext =
System.IO.Path.GetExtension(@"http://www.codedigest.com/Articles/ASPNETAJAX/127_Tips_and_Tricks_-_ASPNet_AJAX_Applications.aspx");
Clearing input controls at a stretch in
ASP.Net
There will be situations where we will
develop an input form for bulk input processing. Obviously, these forms will have large
number of input controls like textbox, dropdownlist, checkbox and radiobutton,
etc. In such forms, we need to reset these control once the input is processed
for the subsequent request. You can use the below code to reset the input
controls at a stretch instead of clearing it one by one.
[Updated code as per the user comments - Thanks to gramotei and Sean]
private void btnClear_Click(object sender,
System.EventArgs e)
{
Control myForm = Page.FindControl("Form1");
foreach (Control ctrl in myForm.Controls)
{
//Clears TextBox
if (ctrl is
System.Web.UI.WebControls.TextBox)
{
(ctrl as TextBox).Text = "";
}
//Clears DropDown Selection
if (ctrl is
System.Web.UI.WebControls.DropDownList)
{
(ctrl as
DropDownList).ClearSelection();
}
//Clears ListBox Selection
if (ctrl is
System.Web.UI.WebControls.ListBox)
{
(ctrl as ListBox).ClearSelection();
}
//Clears CheckBox Selection
if (ctrl is
System.Web.UI.WebControls.CheckBox)
{
(ctrl as CheckBox).Checked = false;
}
//Clears RadioButton Selection
if (ctrl is
System.Web.UI.WebControls.RadioButtonList)
{
(ctrl as
RadioButtonList).ClearSelection();
}
//Clears CheckBox Selection
if (ctrl is
System.Web.UI.WebControls.CheckBoxList)
{
(ctrl as
CheckBoxList).ClearSelection();
}
}
}
Get Control Name in JavaScript
When we use masterpage in ASP.Net, it
will append the ContentPlaceholderId with the ID of the controls that is inside
the Content tag during HTML rendering. For example, when there is a textbox
with ID “txtAccountNumber” it will be rendered as
“ctl00_ContentPlaceHolder1_txtAccountNumber” to the client, where
“ContentPlaceHolder1” is the ID of the Content Placeholder of the Master Page.
This behaviour is again seen when we use user controls.
To access any control in JavaScript we
use,
document.getElementById(' ID')
;
In our case, it will be similar
to,
document.getElementById('
ctl00_ContentPlaceHolder1_txtAccountNumber ')
|