Thursday, September 30, 2010

Get Datarow by PrimaryKey ID and Table Name

Here's the code:

public static DataRow GetTableRow(string ID, string table)
{
string pkSQL = "SELECT column_name FROM
INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE
OBJECTPROPERTY(OBJECT_ID(constraint_name), 'IsPrimaryKey') = 1 AND
table_name = '" + table + "';";
DataTable dtPK = new DataTable();
SqlDataAdapter da = new SqlDataAdapter();
SqlConnection cnConn = new
SqlConnection(ConfigurationManager.ConnectionStrings["myConnections"].Co
nnectionString);
da.SelectCommand = new SqlCommand(pkSQL, cnConn);
cnConn.Open();
da.fill(dtPK);
string pk = Convert.ToString(dtPK.Rows[0][0]);
string stRow = "SELECT * FROM dbo." + table + " WHERE " + pk
+ " = '" + ID + "';";
DataTable dtRow = new DataTable();
SqlDataAdapter daNr = new SqlDataAdapter();
daNr.SelectCommand = new SqlCommand(stRow, cnConn);
da.Fill(dtRow);
cnConn.Close();
cnConn.Dispose();
DataRow myDataRow = dtRow.Rows[0];
//myDataRow =
return myDataRow;
}

Mailto Hyperlink with subject and Body with line breaks

After some trial and error here is sample code for a hyperlink mailto:

HyperLink surveyLink = new HyperLink();
surveyLink.Text = "Send Survey";
StringBuilder sBody = new StringBuilder();
sBody.Append("Please help us provide you with better service
by taking a moment to complete our short survey located at:
%0D%0A%0D%0A");
sBody.Append(surveyUrl + "%0D%0A%0D%0A");
sBody.Append("Thank You,%0D%0A%0D%0A");
sBody.Append("Support Team");
surveyLink.NavigateUrl = "mailto:" + "?subject=Survey&body="
+ Server.HtmlEncode(sBody.ToString()) + " %0D&0A";
placeholder1.Controls.Add(surveyLink);

Wednesday, June 9, 2010

Find row index in a gridview for a row after selecting dropdownlist item

I had a gridview with an item template that has dropdownlist in a
column. When a user changed the dropdownlist selection
I needed to update the record. Problem is the row was not selected so
how do I get the datakey?
On selected index changing you do it like this:

DropDownList myDropDown = (DropDownList)sender;
GridViewRow myRow = (GridViewRow)myDropDown.Parent.Parent;
String myDataKey =
Convert.ToString(myGridView.DataKeys[myRow.RowIndex].Value);

Now you can go do your updates, delete or whatever.

Thursday, May 20, 2010

Method to get values from controls by name

Use this method to get the values from controls on a form by passing in
the ID of the control, like this:

string test = ControlValue(textbox1);

ControlValue code:

public string ControlValue(string controlName)
{
string value = "";
Control c = FindControl(controlName);
if (c is DropDownList)
{
value = ((DropDownList)c).SelectedValue;
}
else if (c is TextBox)
{
value = ((TextBox)c).Text;
}
else if (c is Label)
{
value = ((Label)c).Text;
}
else if (c is ListBox)
{
string list = ",";
foreach (ListItem li in ((ListBox)c).Items)
{
if (li.Selected)
{
list += "," + li.Value;
}
}
value = list.Replace(",,", "");
}
return value;
}

Thursday, April 8, 2010

Make gridview selectable by clicking and changing the cursor on hover

Here is the code to change the cursor as you hover over a gridview row
and enable it to be selected by clicking the row:

protected void gv1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover",
"this.style.cursor='hand'");
e.Row.Attributes.Add("onmouseout",
"this.style.cursor='cursor'");
}
}

protected void gv1_RowCreated(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick",
Page.ClientScript.GetPostBackEventReference(gv1, "Select$" +
Convert.ToString(e.Row.RowIndex)));
}
}

Enjoy.

Pass data from pop-up to parent

Here is a line of code I used to pass a value from a pop-up to the
parent:

string name = "Test";
Page.ClientScript.RegisterStartupScript(this.GetType(), "name", "<script
language=javascript>window.opener.document.getElementById('tbxname').inn
erText = '" + name + "';self.close();</script>");

Enjoy.

Open and center pop-up window

Here is some code I used to open a pop-up window centered with no
address bar:

Put this in the head section:

<script type="text/javascript">
function openpopup(w,h)
{
var left = (screen.width/2)-(w/2);
var top = (screen.height/2)-(h/2);
window.open('frmUserSearch.aspx', '', 'toolbar=no, width='+w+',
height='+h+', top='+top+', left='+left, '');
}
</script>

Then on whatever control is going to open the pop up add:

OnClientClick="openpopup(300, 300)" -----You can change the size to fit
your needs.

Enjoy.