rowcommand doesnt work in first time - asp.net

I have a big mistake in rowcommand. I have a button field with commandname "add". When I click it the code doesn't fire the first time but click it again and the code fires!
if (e.CommandName == "add")
{
DataClassesDataContext db = new DataClassesDataContext();
int ii = int.Parse(e.CommandArgument.ToString());
int num = int.Parse(((TextBox)GridView1.Rows[ii].FindControl("TextBox2")).Text);
string id = GridView1.Rows[ii].Cells[0].Text;
temp t = new temp();
t.tedad = num;
t.username = Session["username"].ToString();
db.temps.InsertOnSubmit(t);
db.SubmitChanges();
}
rowcommand dose not fire when clicking the first time!

You should bound the Datasource to Gridview on postback.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.Datasource = DataTable1;
GridView1.DataBind();
}
}

I figured out the issue, I was using GridView_RowCreated which was causing the problem, instead I used GridView_RowDataBound which solved the problem for me.
or check that if you are binding the datagrid in not post back .

Related

Create a Dynamic DropdownList in ASP.net

I Want to Create a Dynamic Dropdown list in the button Click Event and the drop downs having the
same values
Please Suggest me to get the Solution
Thanks in Advance
Code:
protected void Button1_Click(object sender, EventArgs e)
{
DropDownList dl = new DropDownList();
dl.AutoPostBack = true;
dl.Items.Add("Mike");
dl.Items.Add("John");
dl.Items.Add("Ajai");
dl.Items.Add("Vir");
dl.AutoPostBack = true;
dl.SelectedIndexChanged += new EventHandler(OnSelectedIndexChanged);
form1.Controls.Add(dl);
}

ASP.net : textChange Partial Update for programmatically inserted textboxes

I trying get some programmatically inserted textboxes (inserted into a gridview) to do a textChange partial update. It is sort of working but it does not automatically calls textEntered() method after I typed some text in these textboxes. I got a clue that I might need to use AJAX and things like updatepanels but I just don't fully understand how they will work in the context of what I am trying to do.
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (!e.Row.Cells[4].Text.Equals(" ") && firstTime == false)
{
TextBox tb = new TextBox();
tb.Text = e.Row.Cells[4].Text;
tb.TextChanged += new EventHandler(textEntered);
textBoxArray.Add(tb);
int length = textBoxArray.Count - 1;
tb = (TextBox)textBoxArray[textBoxArray.Count - 1];
e.Row.Cells[4].Text = null;
e.Row.Cells[4].Controls.Add(tb);
Cache["textBoxArray"] = textBoxArray;
} firstTime = false;
}
protected void textEntered(object sender, EventArgs e)
{
lbl_test.Text += "test";//This line is for testing purposes
}
auto postback of textbox is true or false? make it true.

retrieve data key of selected row in gridview in asp.net

I have a button (item template) in any rows of a gridView.
I want when user clicks on this button, I retrieve dataKey of row that there is that button in it.
how can I do this?
(sorry,I can't speak and write English very well)
Assuming you have one DataKey and it is an int and the row of the button isn't necessarily selected and you have connected the following function to the buttons in your template field:
protected void RowClicked(object sender, EventArgs e)
{
Button TempButton = sender as Button;
if (TempButton != null)
{
GridViewRow GVR = TempButton.Parent.Parent as GridViewRow;
if (GVR != null)
{
int ID = (int) GridView1.DataKeys[GVR.DataItemIndex].Value;
}
}
}
Try the SelectedIndexChanged event. Something like this:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e){
TextBox1.Text = GridView1.SelectedRow.Cells[1].Text;
TextBox2.Text = GridView1.SelectedRow.Cells[2].Text;
.
.
.
}

How to programmatically set SelectedValue of Dropdownlist when it is bound to XmlDataSource

I'm using XmlDataSource as the datasource for a dropdownlist.
Now I want to set the SelectedValue of the drop down when the page initially loads. I have tried the OnDataBound event of the drop down in which I could see the total items. But setting the SelectedValue didn't work. InOnDataBinding event, I couldn't even see the total items probably because the list isn't bound yet?
How can I set the selected index based on a value?
This seems to work for me.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DropDownList1.DataBind(); // get the data into the list you can set it
DropDownList1.Items.FindByValue("SOMECREDITPROBLEMS").Selected = true;
}
}
DropDownList1.Items.FindByValue(stringValue).Selected = true;
should work.
This is working code
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DropDownList1.DataTextField = "user_name";
DropDownList1.DataValueField = "user_id";
DropDownList1.DataSource = getData();// get the data into the list you can set it
DropDownList1.DataBind();
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText("your default selected text"));
}
}
Have you tried, after calling DataBind on your DropDownList, to do something like ddl.SelectedIndex = 0 ?

ASP.NET gridview set to null, but reverts to original selection upon doing an add

I have a "Clear" button which sets the gridview datasource to null and binds it. However, when I add a new entry to it, the old rows seem to show up again (in addition to the new row)...not sure what I am doing wrong....
gv.DataSource = null;
gv.DataBind();
Shouldn't the above clear out the state of the gridview?
TIA
protected void btnAddFactor_Click(object sender, EventArgs e)
{
if (txtfactor.Text != "")
{
if (ViewState["factor"] == null)
{
DataTable dtbl = new DataTable();
dtbl.Columns.Add("Factor");
dtbl.Columns.Add("Weight");
DataRow dr = dtbl.NewRow();
dr[0] = txtfactor.Text.Trim();
dr[1] = Convert.ToInt32(ddlWeight.SelectedValue);
dtbl.Rows.Add(dr);
ViewState["factor"] = dtbl;
}
else
{
DataTable dtbl = (DataTable)(ViewState["factor"]);
DataRow dr = dtbl.NewRow();
dr[0] = txtfactor.Text.Trim();
dr[1] = Convert.ToInt32(ddlWeight.SelectedValue);
dtbl.Rows.Add(dr);
ViewState["factor"] = dtbl;
}
gvFactor.DataSource = (DataTable)(ViewState["factor"]);
gvFactor.DataBind();
//gvFactor.Rows.
}
}
protected void btnClearFactor_Click(object sender, EventArgs e)
{
gvFactor.DataSource = null;
gvFactor.DataBind();
}
Are you sure that code is being executed?
If that doesn't run the viewstate gets applied and the row (or anything else you have) doesn't go away.
Please, include the method that contains that code and the related markup.
Update:
Ok, I am looking at the code you added, you saving the stuff in the viewstate, when you clear the gridView and rebind you need to also clear the ViewState (ViewState["factor"] == null), otherwise when you add the other row the old one gets picked up as well as it is still in the viewState:
protected void btnClearFactor_Click(object sender, EventArgs e)
{
ViewState["factor"] == null
// this line you shouldn't need
gvFactor.DataSource = null;
gvFactor.DataBind();
}
If you clear the ViewState setting the DataSource to null is also not needed.

Resources