I have a repeater on my page being populated on page load
con.Open();
SqlDataReader postView = cmd.ExecuteReader();
topicView.DataSource = postView;
topicView.DataBind();
And then I have a section to add a post. What I want it to do is when the user presses add, it reloads the page and displays the updated repeater. However it's not doing this
Everyone has said just reload the databind, except that isn't working as the original databind is in the page load, and the new databind is in a protected void button click.
Can anyone tell me how to do this, or do I just need to place ALL the connection, datasource ad databind info in the button click code?
you can create a function that can be call everytime Button click event fire
Private void BindRepeater()
{
con.Open();
SqlDataReader postView = cmd.ExecuteReader();
topicView.DataSource = postView;
topicView.DataBind();
}
Protected Button_Click(object as sender , EventArgs e)
{
//call the function to populate the repeater with the updated records
this.BindRepeater();
}
you can resolve it like this . create a method
private void Bind()
{
con.Open();
SqlDataReader postView = cmd.ExecuteReader();
topicView.DataSource = postView;
topicView.DataBind();
}
now you can use it anywhere on the page where you need to reload the repeater.
Related
I am trying to create a session when a user clicks a button. I want to update controls on the master.aspx page after the session is created from the login button being clicked. However the master.aspx page_load is being called before the button click function. Any ideas on how I could work around this?
Default.aspx.cs
protected void ButtonLoginIn_Click(object sender, EventArgs e)
{
Session["username"] = "Joe";
}
Master.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"] == null)
{
LinkLoginInOut.Text = "Sign In";
}
else
{
string sessionId = Session["username"].ToString();
LabelDbUserName.Text = "Joe";
LinkLoginInOut.Text = "Log Out";
}
}
You can simply use FindControl on the Master
LinkButton lb = Master.FindControl("LinkButton1") as LinkButton;
lb.Text = "LinkButton Master";
You can try to move the code in the Page_Load event of the masterpage to an event, that is called after the button_click event. That way the changes to the session occur before the updating of the masterpage. You can try the Page_LoadComplete event. Please check the "asp.net page life cycle" documentation for the sequence, in which the different page events are called.
I am inserting data to database when i click submit button data inserted into Database but no changes get reflected in GridView without refreshing the page.
I used DataBind() also.
it is enough to add
YourGridView.DataBind();
in your button onclick event.... no need to bind it also in Page_Load
Do you have any updatepanels?
Just use the Procedure which populates the GridView and then call that Procedure OnClick Event.
Hope it Helps
You can use this code - based on DataBind two times
Nota : you add this DataBind no just in your Page_Load but also in your delegate of click
//Your Insert in your delegate
....
YourGridView.DataBind();
You have to refresh the gridviews data source through whatever means your using, for instance an sql query and then set the datasource to this and then use Gridview1.DataBind();
public void GetData()
{
try
{
SqlConnection connection = new SqlConnection();
connection.ConnectionString = ConfigurationManager.ConnectionStrings["mysql"].ConnectionString;
String sql = "Your Query";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sql, connection);
da.Fill(ds);
DataTable dt = new DataTable();
dt = ds.Tables[0];
GridView1.DataSource = dt;
}
catch (SqlException ex)
{
}
catch (Exception e)
{
}
}
Then just use GetData() before you bind the gridview (possibly in your page load event).
You can also do in your rowCommand:
gridView_RowCommand(object sender, GridViewCommandEventArgs e){
gridView.EditIndex = -1; // to cancel edit mode
}
I am trying to submit filledin information in the form to the database. I have done the below mentioned coding for the save button. When I click on the save button to save the information, I am getting an error. I need it to be done and for the same need help from the geeks around here.
COde for SAVE button data from database
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string conn = ConfigurationManager
.ConnectionString("welcome").ConnectionString;
SqlConnection con=new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("Insert into abhishek(pr_name,Add,add2) values(#ah,#Ap,#qe)",con);
cmd.CommandType = CommandType.Text;
cmd.Parameter.Addwithvalue("#ah", TextBox1.Text.Trim());
cmd.Parameter.Addwithvalue("#ap", TextBox2.Text.Trim());
cmd.Parameter.Addwithvalue("#qe", TextBox3.Text.Trim());
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
Check the error in the image below:
You are missing an 's' from the ConnectionStrings indexed property. Once you fix that error, however, you will also find you are missing more 's' from the SqlCommand.Parameters property.
To reveal these issues, open Properties of your code-behind file, and choose Build Action of Compile. (Also, I can see from your image that it looks like your project is setup as a Web Site rather than a Web Application; if possible, look into converting this over to a Web Application, as it offers a number of benefits.)
Once you get your syntax corrected, then you should change your code so that the update is invoked through the button click handler, rather than on page load.
In your markup code in the aspx:
<asp:Button runat="server" ID="btnSave" Text="Save" OnClick="btnSave_Click" />
In your code-behind:
protected void btnSave_Click(object sender, EventArgs e)
{
string conn = ConfigurationManager.ConnectionStrings["welcome"].ConnectionString;
using (SqlConnection con = new SqlConnection(conn))
{
SqlCommand cmd = new SqlCommand("Insert into abhishek(pr_name,Add,add2) values(#ah,#Ap,#qe)", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("#ah", TextBox1.Text.Trim());
cmd.Parameters.AddWithValue("#ap", TextBox2.Text.Trim());
cmd.Parameters.AddWithValue("#qe", TextBox3.Text.Trim());
con.Open();
cmd.ExecuteNonQuery();
}
}
Note that I have moved the code into the button click event handler, and out of the Page_Load. The Load event handler should only be used for initialization items needed unconditionally on the execution path of the page (i.e., regardless of whatever button was clicked on the page), and should look something like the following:
protected void Page_Load(object sender, EventArgs e)
{
// Code to run every time the page is created, both initially and on postback
if (IsPostBack)
{
// Code to run every time the page is posted back (any submit event)
}
else
{
// Code to run on the initial creation of the page
}
}
I have a formview that on page load makes a call to a sql server and retrieves 5 records which I want the formview to paginate though.
I have sucessfully connected to the db, filled a dataset, and returned data when the web page renders. The problem is that when I move to a new page, the data does not change in the databound field.
Here is my code:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
conn = new SqlConnection(connstr);
ds = new DataSet();
da = new SqlDataAdapter("call to stored proc", conn);
try
{
conn.Open();
da.Fill(ds, "m");
FormView1.DataSource = ds;
FormView1.DataKeyNames = new string[] { "PropKey" };
FormView1.DataBind();
}
catch (Exception ex)
{
Result = ex.Message;
}
finally
{
conn.Close();
}
}
Next when the paginate buttons are clicked I have this:
protected void FormView1_PageIndexChanging1(object sender, FormViewPageEventArgs e)
{
FormView1.PageIndex = e.NewPageIndex;
}
Please help,
Thanks
You will need to bind the data to the FormView just after setting the new page index like below.
protected void FormView1_PageIndexChanging1(object sender, FormViewPageEventArgs e)
{
FormView1.PageIndex = e.NewPageIndex;
BindFormViewData();
}
This is required because the FormView only displays the data for the active record and does not store any other records from the datasource, so upon change of the page index, the datasource is required to be bound again. See: Paging in a FormView Web Server Control
From the above link:
If the FormView control is bound to a data source control, or to any
data structure that implements the ICollection interface (including
datasets), the control gets all the records from the data source,
displays the record for the current page, and discards the rest. When
the user moves to another page, the FormView control repeats the
process, displaying a different record.
Hope this helps.
I have an ASP.NET webform on which I use a DropDownList control to allow the user to select an item and see related results. For some reason when I set the SelectedValue property of the DropDownList the value it's set to is not immediately available.
Here's my code:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DropDownList1.SelectedValue = "5";
BindView();
}
}
protected void BindActivities()
{
DataClassesDataContext dc = new DataClassesDataContext();
var query = from activity in dc.Activities
where activity.AssignedTo == Convert.ToInt32(DropDownList1.SelectedValue);
GridView1.DataSource = query;
GridView1.DataBind();
}
In the previous code I get an error that DropDownList1.SelectedValue is null. The wierd thing is that if I comment out the code that uses DropDownList1.SelectedValue and let the page load, DropDownList1 is actually set to value 5. So it looks like it's getting set correctly but is just not immediately available. The debugger confirms that DropDownList.SelectedValue is not set to 5 immediately after the line of code that sets it.
Any ideas what is going on here?
Are you setting the value before you have bound the dropdown list?
Yes the user above is right
if (!Page.IsPostBack)
{
BindView();
DropDownList1.SelectedValue = "5";
}
... should work just fine.
There is no such thing as a delay in execution, just the order of execution.