Sunday, October 25, 2009

Clear default text onClick - restore if nothing entered

Let's write a JavaScript function which will clear the "default" text in a field when the user clicks into it and the second function will replace the default text in the field if the field was left blank.

Put this inside the <head> </head> tag:

<script type="text/javascript">
function clickclear(thisfield, defaulttext) {
if (thisfield.value == defaulttext) {
thisfield.value = "";
}
}

function clickrecall(thisfield, defaulttext) {
if (thisfield.value == "") {
thisfield.value = defaulttext;
}
}
</script>

Then add the following onclick, onblur events to your field:

<asp:TextBox ID="rajTextBox" runat="server" Text="Rajendra Sedhain"
Width="369px" Height="18px" onclick="clickclear(this, 'Rajendra Sedhain')" onblur="clickrecall(this,'Rajendra Sedhain')"></asp:TextBox>

Happy Programming !!!

Monday, October 5, 2009

Disabling an ASP.Net Validator through Javascript

Let say, I have two dropdown lists, both have required field validation controls and use Javascript to hide the second dropdown when choose the specific items from the first dropdown list. It works fine but the problem is when choose the specific items from the first dropdown it hides the second one but it still displays the required field validator.

So, the solution:

When we hide, we can disable the validator at the same time using ValidatorEnable().

function validate()
{

var myValidator = document.getElementById('myValidatorClientID');
ValidatorEnable(myValidator, false);

}

To make enable replace that 'false' by 'true'.

Happy Programming !!!

Friday, September 25, 2009

Change Hidden Field Value

Let say, you have a Textbox with some value. When you choose some items from a dropdown list, this textbox will be hidden but the value will be there. When you submit the form, this textbox's value will be submitted.

To avoid this, we can use javascript so that when the textbox hidden, we can make it empty or assign something specific value whatever you want.




function assignSomeValue()
{
// to hide the textbox
document.getElementById('<%= rajTextBox.ClientID%>').style.visibility = "hidden";
// to assign somevalue after hidden
documnet.getElementById('<%= rajTextBox.ClientID%>').value="assign something";


}





Happy Programming !!!

Saturday, September 5, 2009

Calling Stored Procedure from C# & Display the records

Let's create a sample stored procedure first,

Create procedure GetSchoolName (@ID int)

as

select SchoolName, Date from School where ID like @ID;

return;

The code includes @ID parameter which is an input parameter that obtains the search string to perform a "like" search in school table.

In our c# code, we are going to pass variable ID (at run time) to this procedure and getting back the records that matches with that ID

Hence, the c# code:

try
{
SqlDataReader rdr = null;

//Create a connection to the SQL Server
SqlConnection conn = new SqlConnection(DataAccess.GetConnectionString());

//Create a command object & then set to the connection
SqlCommand cmd = new SqlCommand("dbo. GetSchoolName", conn);

//Set the command type as storedProcedure
cmd.CommandType = CommandType.StoredProcedure;

//get the variable ID from textbox control
int id = Convert.ToInt32(id.Text);

//Create & add a parameter to parameters collection for stored procedure
cmd.Parameters.Add("@ID", SqlDbType.Int).Value = id;

//Open the connection
conn.Open();

//execute command & read the data using SqlDataReader
rdr = cmd.ExecuteReader();


//display records into listbox

while (rdr.Read())
{
listBox1.Items.Add(rdr.GetValue(0).ToString());
}
Or

//Records display in a table

TableRow tr;
TableCell tc;
while (rdr.Read())
{
tr = new TableRow();
tc = new TableCell();
tc.Text = rdr["SchoolName"].ToString() + " " + rdr["Date"].ToString();
tr.Cells.Add(tc);
table1.Rows.Add(tr);

}

conn.Close(); //close connection
rdr.Close(); // close SqlDataReader
}

Happy Coding !!!