update contact info based on input fields through tsql - c#

I have a web application that I am developing that has an 'my profile' page. This page grabs all the users info and populates the appropriates fields.
Certain details have been blacked out for privacy reasons. This is working great. What i want to achieve is the ability for the user to edit any of these text fields, and hit the 'update information' button. I am not sure how to do this.
Here is my code behind for what I have working so far.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
grabData();
}
}
protected void grabData()
{
HttpCookie cookieLogin = Request.Cookies[Constants.CK_CAUSE_MARKETER_ID];
string sql = "SELECT * " +
"FROM CauseMarketers m with(nolock)" +
"INNER JOIN CauseMarketer_Contacts t with(nolock) ON t.CauseMarketerID = m.CauseMarketerID " +
"INNER JOIN CauseMarketer_Companies q ON q.CauseMarketerID = m.CauseMarketerID " +
"WHERE m.CauseMarketerID = " + cookieLogin.Value;
DataTable Results = Utils.SQLUtils.GetDataTable("TFOUNDATION", sql);
DataRow FirstResult = Results.Rows[0];
userFirstName.Text = FirstResult["FirstName"].ToString();
userLastName.Text = FirstResult["LastName"].ToString();
userCompanyName.Text = FirstResult["CompanyName"].ToString();
userAddress.Text = FirstResult["Address1"].ToString();
userAddress2.Text = FirstResult["Address2"].ToString();
userCity.Text = FirstResult["City"].ToString();
userZip.Text = FirstResult["ZipCode"].ToString();
}

Implement update information button click event.
<asp:Button ID="UpdateId" runat="server" Text="Update Information" OnClick="UpdateId_Click" />
If above the asp.net button then button client event look something like below.
protected void UpdateId_Click(object sender, EventArgs e)
{
// write update sql.
}
During first page load anyway information will get loaded based on your code. When user clicks on button this click event gets fired and you can capture all form information and send to db for update.

Related

Keep original Attributes on ImageButton after tampering with it on ASP.NET C#

I want to enable a function that whenever you click an imagebutton, it automatically switches to a loading image which also disables it so the user cannot click multiple times until it finishes it's process.
I do the following:
protected void Page_Load(object sender, EventArgs e)
{
ibtnCancel.Attributes.Add("onclick", DisableButton(ibtnCancel));
ibtnSave.Attributes.Add("onclick", DisableButton(ibtnSave));
}
protected string DisableButton(ImageButton objImageButton)
{
string strAttribute = GetScript();
strAttribute += "this.value = 'PROCESSING...';";
strAttribute += "document.getElementById('" + objImageButton.ID + "').disabled = true;";
strAttribute += "document.getElementById('" + objImageButton.ID + "').src = '../images/saveInactive.png';";
strAttribute += Page.GetPostBackEventReference(objImageButton) + ";";
return strAttribute;
}
However, I have noticed that some of the attributes that were specified on the web form are no longer working, for instance:
<asp:ImageButton ID="ibtnCancel" runat="server" ImageUrl="~/images/cancelar.png" CausesValidation="False" onclick="ibtnCancel_Click" />
CausesValidation is no longer working, so that means that if there is a validator on the form that would be triggered, it appears when I click this button despite I try to tell it not to.
How could I turn this around?, anything that I could edit on the method specified?
Use OnClientClick property of ImageButton instead http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.imagebutton.onclientclick%28v=vs.110%29.aspx

QueryString to Textbox

I have a table tblProfile, which contains fields with Id, Name, Address. I have 2 aspx page, which are Home.aspx and Here.aspx. On my Home.aspx I have used this code to pass the Id in my Here.aspx:
<asp:HyperLink ID="link" runat="server" NavigateUrl="Here.aspx?id={0}"
Font-Names="Times New Roman" Font-Size="Medium" >Click here</asp:HyperLink>
In code behind Home.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
string bID = Request.QueryString["Id"];
if (string.IsNullOrEmpty(Id))
{
Response.Redirect("Here.aspx?", true);
}
ViewState["Id"] = Id;
link.NavigateUrl = String.Format(link.NavigateUrl, Id);
}
I don't have any problem with passing the Id to url in 2nd page. But what I want right now is, on my Here.aspx, I have 3 textboxes which supposed to be filled by the Id, Name and Address of the certain Id that passed from the Home.aspx. Tried many but had no luck at all. Any help would be appreciated. By the way, I'm using asp.net with c#.
We are having two solution for it
Solution 1 : User Previous page property in asp.net so that you no need to pass ID. i.e.
protected void Page_Load(object sender, EventArgs e)
{
// Find the name on the previous page
TextBox txt = (TextBox)Page.PreviousPage.FindControl("txtNameText");
if (txt != null)
txtName.Text = Server.HtmlEncode(txt.Text);
else
txtName.Text = "[Name Not available]";
}
Solution 2 : Get ID from query string and get Data by ID in assign on text box text property.i.e.
protected void Page_Load(object sender, EventArgs e)
{
// Get value from query string
string profileID = Request.QueryString["Id"];
// Fetch data from database by ID
DataTable dt = FetchEmployeeDataByID(ProfileID);
txtName.text = dt.rows[0]["Name"].tostring();
}
From your reply on comments, I suggest to do the following:
Load your data in Here.aspx back-end (.cs file) by the Id that you retrieve from query string
Show the loaded data into your desired textbox.
You can get the name and address by retrieving the profile data from the database using the Id from the query string as the key.
So in your Here.aspx's code behind:
protected void Page_Load(object sender, EventArgs e)
{
string profileID = Request.QueryString["Id"];
// Retrive profile in your db..
var profile = Profiles.Get(p => p.ProfileID == profileID);
// Populate the textboxes
txtId.Text = profileID;
txtName.Text = profile.Name;
txtAddress.Text = profile.Address;
}

Search on gridView does nothing

I have a gridVIew which has a datasource of a table in Access database.
I want to add search functionality on this gridview. I added a textBox and search button. My Code is
protected void btnSearchService_Click(object sender, EventArgs e)
{
string SearchField = TextBox1.Text;
string searchSQL = "SELECT * FROM LocalService WHERE ServiceName LIKE '%" + SearchField + "%'";
SqlDataSource1.UpdateCommand = searchSQL;
SqlDataSource1.Update();
}
But in browser when I click Search button, I see nothing happens. DO I have to add code on page load or somewhere?
It worked. I have replaced following lines
SqlDataSource1.UpdateCommand = searchSQL;
SqlDataSource1.Update();
with these lines
SqlDataSource1.SelectCommand = searchSQL;
GridView1.DataBind();

Passing values from a class to a method of another class

I am creating a website. The home page has a text box and drop down box in which the user enters the movie name and language to search. When the user clicks on the Search button the search result page is displayed and the results of search should be displayed in a data grid. I created session variables to pass the text of the text box and data grid to be used in the other page. The code to fetch data from the database is in a class how do i pass the values received from the database to a method of another page? This is the code i have written, it doesn't give any errors but the data grid does not get filled with results what am I doing wrong?
//Code for search button in home page
protected void Btnsearch_Click(object sender, EventArgs e)
{
Response.Redirect("SearchResults.aspx");
Session["moviename"] = TextBox3.Text;
Session["language"] = DropDownList1.Text;
}
//Code to fetch data from database
public class movie
{
public SqlDataAdapter searchmovie(object moviename, object language)
{
connection.con.Open();
SqlDataAdapter adapter1 = new SqlDataAdapter("select
select movieName,language,director,price from movie
where moviename = '" + moviename + "' and
language = '" + language + "'",
return adapter1;
}
}
//Code in search page to fill data grid with search results
public partial class SearchResults : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
movie m = new movie();
SqlDataAdapter movieDetails = m.searchmovie(Session["moviename"],
Session["language"]);
DataSet data = new DataSet();
movieDetails.Fill(data, "movieD");
GridView1.DataSource = data.Tables["movieD"];
GridView1.DataBind();
}
}
Set the session variables before redirecting, like this:
protected void Btnsearch_Click(object sender, EventArgs e)
{
Session["moviename"] = TextBox3.Text;
Session["language"] = DropDownList1.Text;
Response.Redirect("SearchResults.aspx");
}
I would like to suggest to avoid Session as data storage.
ASP.NET has a nice feature called cross-posting: it make you able all the page control and state from a page to another.
http://msdn.microsoft.com/en-us/library/ms178139.aspx
Personally, I really love feature because you can refer to page as object, having controls exposed ad property!

Problems submitting dynamic checkbox values

I'm working on creating a dynamic list of checklists and came to the below implementation. I have been trying for a week now to get the fnameID text values on the submit button click to send the values into the database. I do not want to use the postback oncheckedchanged.on each check because the checklist is over 1000 rows long and each postback/reload wastes too many resources. I just want to be able to use some method to be able to grab the checked values so I can insert them into the database on the submit button "Click Me!" click.
I googled and found the FindControl method but i still am not able to grab the fnameID values. I either get a undefined or it errors out on me. Any help would be greatly appreciated! Thanks!
aspx:
<div id="aGAccountabilityCheckListBox">
"Panel1" runat="server">
<asp:LinkButton ID="LinkButton1" Width="66px" runat="server" onclick="LinkButton1_Click">
Click Me!
</asp:LinkButton>
code behind:
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 50; i++)
{
CheckBox _checkbox = new CheckBox();
_checkbox.ID = "dynamicCheckListBox" + Convert.ToString(i);
Panel1.Controls.Add(_checkbox);
Panel1.Controls.Add(" <span id='fnameID" + i + "' >test" + i + "</span>");
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["cnDatabase"].ToString());
SqlCommand cmd = new SqlCommand("usp_CreateUser", cn);
cmd.CommandType = CommandType.StoredProcedure;
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
Thanks!
State is restored to controls before the load event runs. If your controls don't already exist by then, they'll lose their state and you won't know they were checked. Create your checkboxes in the Init or PreInit event instead.
move the checkbox creation to the CreateChildControls page method
to retrieve checkbox state in the LinkButton1_Click handler you can use the following code
for (int i = 0; i < 50; i++)
{
string cbxId = string.Format("dynamicCheckListBox{0}", i);
CheckBox _checkbox = Panel1.FindControl(cbxId) as CheckBox;
if (_checkbox == null )
continue;
if ( _checkbox.Checked )
//do something
}
Your fnameID's are spans created as a literal control. There is no post back value you are going to get if you set it up that way. It's just arbitrary html or text from the asp.net perspective.
Why are you not using the Text property for CheckBox?

Categories