Tuesday, July 29, 2008

Character limit on a multi line text box

//Write code on page load event

TextBox1.Attributes.Add("onkeydown", "return checkcharacter(this,50)");

//write javascript function between head section on html page

<script language="javascript">
function checkcharacter(txt,maxLen){
try{
if(txt.value.length > (maxLen-1))
return false;
else if( txt.value.length == (maxLen-1))
alert('you can enter only 50 character !!!!!!!! ');
}
catch(e){
}}
</script>

Friday, July 11, 2008

Delete multiple record from gridview by single database interaction

//write code on page load
if (!IsPostBack)
{
binddata();
}

// Bind Function

public void binddata()
{
string strSql = " select * from test_test";
ds1 = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter(strSql, con);
adp.Fill(ds1, "aa");
GridView1.DataSource = ds1.Tables[0];
GridView1.DataBind();
}

// write code in button click

StringBuilder store_id = new StringBuilder();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox c = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (c.Checked)
{
store_id.Append(GridView1.Rows[i].Cells[1].Text+"','");
}
}
string c_id = store_id.ToString();
string ss = "delete from test_test where c_id in ('"+c_id+"')";
SqlCommand cmd = new SqlCommand(ss,con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
binddata();