Datakeynames, request.querystring - asp.net

I am doing something wrong in this small code
Page1: `
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
UserType = DDlUserType.SelectedItem.Text;
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Get the value in the hyperlink column.
string HyperLinkValue = e.Row.Cells[1].Text;
HyperLink myLink = new HyperLink();
myLink.NavigateUrl = "~/ShowMMBProfileStats1.aspx?Profile_ID={0}";
myLink.Text = HyperLinkValue;
}
In ShowMMBProfileStats1.aspx
protected void Page_Load(object sender, EventArgs e)
{
int MMBProfileID = Convert.ToInt32(Request.QueryString[0]);
}
It gives me an error
Input string was not in a correct
format.
In the aspx page, I am assigning datakeynames="Profile_ID"
How do I carry this Profile_ID to page1.
`
Thanks
Sun

Try the following:
myLink.NavigateUrl =
"~/ShowMMBProfileStats1.aspx?Profile_ID=" + e.Row.Cells[profileIDCellIndex].Text;

You're not setting the value for {0}. Try something like this:
myLink.NavigateUrl =
String.Format("~/ShowMMBProfileStats1.aspx?Profile_ID={0}", HyperLinkValue );
or
myLink.NavigateUrl = "~/ShowMMBProfileStats1.aspx?Profile_ID=" + HyperLinkValue ;

Related

Add autogenerated Column to gridview

i have AutoGenrated SerialNo of my Gridview in RowDatabound event like below..
protected void Gdview_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblSerial = (Label)e.Row.FindControl("lblSRNO");
lblSerial.Text = "S" + ((Gdview.PageIndex * Gdview.PageSize) + e.Row.RowIndex + 1).ToString();
}
}
Now i Want to Add this Text to my SQL Column..this SeirlNo is first Column ,,i followed Below procedure to add the values to the Sql Table...
protected void Button1_Click(object sender, EventArgs e)
{
string strOrganization = txtOrganization.Text;
string strOrigin = txtOrigin.Text;
string strLoc = TexLocation.Text;
string strService = TxtService.Text;
string strEst = TxtEstablish.Text;
//SqlCommand cmd = new SqlCommand ("Insert into mpSoftware Organization='"+strOrganization+"'",Origin='"++"',Location='"++"',Service='"++"',Est='"++"' Where SerialNo='"++"');
.....
....
...
But here i strucked..i fogotted that SerialNo is autogenrated..how wud i get the SerialNo value here???? Give me some valuable suggestions plz...
}
Add a public property to hold the value of the Serial No when it is generated. Then pass that value into your sqlCommand

Adding a checkbox in a table cell

Is this possible?
I have a table with user accounts retrieved from a database. Then at the start of each column I would like to add a checkbox which if selected will select the account. I tried experimenting with it and I can't seem to put the checkbox inside the table. How can I do this?
You can try this:
const int ColumnSelect = 0;
protected void Page_Load(object sender, EventArgs e)
{
//Get real data here.
DataTable dt = new DataTable();
dt.Columns.Add("count");
dt.Rows.Add(dt.NewRow());
dt.Rows[0][0] = "5";
GridView1.Columns.Add(new TemplateField());
BoundField b = new BoundField();
GridView1.Columns.Add(b);
b.DataField = "count";
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.Header)
{
e.Row.Cells[ColumnSelect].Controls.Add(new CheckBox());
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach(GridViewRow row in GridView1.Rows)
{
//Could also use (CheckBox)row.Cells[ColumnSelect].FindControl if you give the checkboxes IDs when generating them.
CheckBox cb = (CheckBox)row.Cells[ColumnSelect].Controls[0];
if (cb.Checked)
{
//Do something here.
}
}
}

How can I display User Input to another page

How can I display user input if I have this example.
(Default.aspx)
FirstName: (textbox)
Last Name: (textbox)
I wanted to display the user input coming from Default.aspx to another page (WebForm1.aspx)
using Response.Redirect.
How can I do this ?
Is
Response.Redirect("WebForm1.aspx?VariableName" + TextBox1.Text)
and TextBox1.Text.Request.QueryString("VariableName")
working ?
Yes, it will work but you need to add = after your variable name as below
Response.Redirect("WebForm1.aspx?VariableName1=" + TextBox1.Text + "&VariableName2="+ TextBox2.Text);
and also in yourWebForm1.aspx
TextBox1.Text = Request.QueryString["VariableName"];
TextBox2.Text = Request.QueryString["VariableName2"];
Page one:
protected void Button1_Click(object sender, EventArgs e)
{
String strTxt1 = TextBox1.Text.ToString();
String strTxt2 = TextBox2.Text.ToString();
Response.Redirect("~/new_name.aspx?fName=" + strTxt1 + "&lName=" + strTxt2);
}
Page two:
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Request.QueryString["fName"].ToString();
Label2.Text = Request.QueryString["lName"].ToString();
}

second page does not fetch correct data in C#

First page :
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var firstCell = e.Row.Cells[0];
firstCell.Controls.Clear();
firstCell.Controls.Add(new HyperLink { NavigateUrl = "ser_job_status1.aspx?Complaint_No = " + firstCell.Text, Text = firstCell.Text, Target = "_blank" });
Session["Complaint_No"] = firstCell.Text;
}
}
Second page :
protected void Page_Load(object sender, EventArgs e)
{
string strComplaintNo = Convert.ToString(Session["Complaint_No"]);
TextBox51.Text = strComplaintNo;
}
TextBox.51.Text of my second page fetches value from First page session.
My problem is my textbox51.text fetches value of Complaint_No which is bound finally to gridview rather fetching my hyperlinked value..
Kindly help to solve this issue..
Session["Complaint_No"] = firstCell.Text; is set in your session, so value of firstCell.Text must be fetched in the second page.
change
Session["Complaint_No"] = firstCell.Text;
to
Session["Complaint_No"] = ((HyperLink)firstCell.Controls[0]).NavigateUrl;

radcombobox always selecting top item on postback

I have an edit page where I set the selected index of a radcombobox (rcb_ParentCompany) based on a value returned from the database. However on postback the text in the combobox keeps changing to the top item in the dataset. Any ideas why?
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
BindOperatingNameComboBox(rcb_OperatingName);
BindParentCompanyComboBox(rcb_ParentCompany);
}
}
protected void btn_Edit_Command(object sender, CommandEventArgs e)
{
Client ClientToEdit = ClientController.ViewClient(int.Parse(e.CommandArgument.ToString()));
//Populate Client fields
txt_ClientName.Text = ClientToEdit.ClientName;
rcb_OperatingName.Text = ClientToEdit.OperatingName;
int ParentCompanyIndex = rcb_ParentCompany.FindItemIndexByValue(ClientToEdit.ParentCompanyID.ToString());
rcb_ParentCompany.SelectedIndex = ParentCompanyIndex;
txt_Address1.Text = ClientToEdit.Address1;
txt_Address2.Text = ClientToEdit.Address2;
txt_Country.Text = ClientToEdit.Country;
txt_Region.Text = ClientToEdit.Region;
txt_City.Text = ClientToEdit.City;
txt_PostalCode.Text = ClientToEdit.PostalCode;
txt_ClientNote.Text = ClientToEdit.ClientNote;
tbl_EditServices.Controls.Clear();
PopulateEditClientPanel(ClientToEdit);
btn_SaveChanges.CommandArgument = e.CommandArgument.ToString();
btn_Cancel.CommandArgument = e.CommandArgument.ToString();
}
protected void BindParentCompanyComboBox(RadComboBox ComboBox)
{
DataTable OperatingNames = ClientController.GetExistingClientAndOperatingNames("");
ComboBox.DataTextField = "ClientName";
ComboBox.DataValueField = "ClientID";
ComboBox.DataSource = OperatingNames;
ComboBox.DataBind();
}
protected void rcb_ParentCompany_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
{
BindParentCompanyComboBox((sender as RadComboBox));
}
Any ideas why?
Yes, because you are doing if(IsPostBack) as opposed to if(!IsPostBack)

Resources