Have a fun while searching

Thursday, May 26, 2011

check limit in datalist checkbox

----------------------script------------------------
script type="text/javascript"
// this is for the limit of how many checkboxes can be checked in datalist
function chkCount(obj) {
if (obj.checked == true) {
if (document.getElementById('gt;%=hiddenChkCount.ClientID %lt;').value >=2) {
alert('You cannot select more than 2 items.');
obj.checked = false;
}
else {
document.getElementById('gt;%=hiddenChkCount.ClientID %lt;').value = parseInt(document.getElementById('gt;%=hiddenChkCount.ClientID %lt;').value) + 1;
}
}
else {
document.getElementById('gt;%=hiddenChkCount.ClientID %lt;').value = parseInt(document.getElementById('gt;%=hiddenChkCount.ClientID %lt;').value) - 1;
}
}
/script
----------------------aspx page---------------------------------------

-------------------c#.net -----------------------------------
protected void dlShowWelcomeBooks_ItemDataBound(object sender, DataListItemEventArgs e)
{
CheckBox chk = default(CheckBox);
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// get reference to checkbox control placed inside the first cell of gridview control
chk = (CheckBox)e.Item.FindControl("checkBooks");
// add onclick event to the checkbox to call javascript validation function
chk.Attributes.Add("onclick", "chkCount(this)");
}
}

Tuesday, May 3, 2011

Modify query string dynamicaly without effecting other

using System.Reflection;
using System.Collections.Specialized;
+++++++++++++++++++++++++++++++++++++++++++++++++++
//Modify the query string at run time
string path = HttpContext.Current.Request.Url.AbsoluteUri;
string[] strarr = path.Split('?');
if (strarr.Length > 1)
{
if (strarr[1].Contains("JobType"))
{
// reflect to readonly property
PropertyInfo isreadonly =

typeof(System.Collections.Specialized.NameValueCollection).

GetProperty("IsReadOnly", BindingFlags.Instance |

BindingFlags.NonPublic);

// make collection editable


isreadonly.SetValue(this.Request.QueryString, false, null);

// modify
this.Request.QueryString.Set("JobType",

Convert.ToString(e.CommandName));

// make collection readonly again


isreadonly.SetValue(this.Request.QueryString, true, null);

Response.Redirect(strarr[0] + "?" +

this.Request.QueryString, false);
}
else
{
Response.Redirect(path + "&JobType=" +

Convert.ToString(e.CommandName), false);
}
}
else
{
Response.Redirect(path + "?JobType=" +

Convert.ToString(e.CommandName), false);
}