Asp.net Repeater Checkbox ID and Value - asp.net

I would like to get the value and id of a checkbox in a repeater, and save the value in database.
Here is what I have;
Repeater;
I have an id like this ; <%# DataBinder.Eval(Container.DataItem, "ID")%>">
the checkbox i want to use
<asp:CheckBox ID="chkRemind" runat="server" OnCheckedChanged="Check_Changed" AutoPostBack="true" />
code behind
protected void Check_Changed(Object sender, EventArgs e) {
I need to know the Id and find out the current checkbox value
so i can do
UpdateDB(ID, checkboxValue);
}

You can databind the id to a data- attribute, then read that on postback.
cb.Attributes["data-id"] = DataItem.ID;
protected void Check_Changed(Object sender, EventArgs e) {
var checkbox = sender as CheckBox;
int ID = int.Parse(checkbox.Attributes["data-id"]);
UpdateDB(ID, CheckBox.Checked);
}
Make sure you verify that the current user has permission to edit the requested item, since the data-id value can be changed on the client.

Do this:
protected void Check_Changed(Object sender, EventArgs e) {
var item = sender as Checkbox;
UpdateDB(item.ID, item.Value);
}

Variable sender contains Control, that triggered event, so you need just cast it to CheckBox
protected void Check_Changed(Object sender, EventArgs e)
{
CheckBox chbSomeCheckbox = (CheckBox)sender;
var ID = chbSomeCheckbox.ID;
var checkboxValue = chbSomeCheckbox.Checked;
UpdateDB(ID, checkboxValue);
}

All you have to do is get sender control's ID
protected void Check_Changed(Object sender, EventArgs e)
{
Checkbox item = sender as Checkbox;
UpdateDB(item.ID, item.Value);
}

Related

GridView | drop down lists gets unpopulate

The Drop Down Lists in the FooterTemplate gets unpopulate when clicking "update" on some row
this is the page load event when they gets populate:
protected void Page_Load(object sender, EventArgs e)
{
DropDownList ddlImages_new = ((DropDownList)gvAdminArticleAdd.FooterRow.FindControl("ddlImages_new"));
ddlImages_new.DataSource = GetPdfs();
ddlImages_new.DataBind();
DropDownList ddl_invNamesNew = ((DropDownList)gvAdminArticleAdd.FooterRow.FindControl("ddl_invNamesNew"));
ddl_invNamesNew.DataSource = GetInvestigatorNames();
ddl_invNamesNew.DataBind();
}
If I click the update linkButton on some row the data in the drop down lists are disappear
Even when try to call the page load on cancel event it didn't work.
protected void gvAdminArticleAdd_CancelEditEventHandler(object sender, GridViewCancelEditEventArgs e)
{
Page_Load(sender, e);
}
Bind your controls only when the page is not post back:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList ddlImages_new = ((DropDownList)gvAdminArticleAdd.FooterRow.FindControl("ddlImages_new"));
ddlImages_new.DataSource = GetPdfs();
ddlImages_new.DataBind();
DropDownList ddl_invNamesNew = ((DropDownList)gvAdminArticleAdd.FooterRow.FindControl("ddl_invNamesNew"));
ddl_invNamesNew.DataSource = GetInvestigatorNames();
ddl_invNamesNew.DataBind();
}
}

Parameters through onclick event in asp

I've been trying to pass parameters through the onclick event using asp as follows:
<asp:Button runat="server" ID="btnUpdateFacturaID" style="display:none;" onclick="btnUpdateFacturaID_Click" CommandArgument="test" />
And on the other side:
protected void btnUpdateFacturaID_Click(object sender, CommandEventArgs e)
{
string s = e.CommandArgument.ToString();
}
But I receive an error of no overload for 'btnUpdateFacturaID_Click' mathes delegate 'System.EventHandler'
In fact, I had initially the functions as follows:
protected void btnUpdateFacturaID_Click(object sender, EventArgs e)
{
string s = e.CommandArgument.ToString();
}
But this way I can't (or I don't actually know, which is much more probable) pass parameters through the event. What amb I missing?
Try this :
protected void btnUpdateFacturaID_Click(Object sender, EventArgs e)
{
Button btn = (Button)sender;
//do whatever with
//btn.CommandArgument.ToString()
}

Textbox Text change Event

I have a form and I am want to call a stored procedure on text change event of that text-box ,
Can you suggest any idea.
Thanks in advance.
You just have to handle the TextChanged-event:
<asp:TextBox ID="TextBox1" runat="server"
OnTextChanged="TextBox1_TextChanged">
</asp:TextBox>
Codebehind:
protected void TextBox1_TextChanged(Object sender, EventArgs e)
{
TextBox txt = (TextBox) sender;
// call your stored-procedure
}
Note that you should databind the controls only if(!IsPostBack), otherwise the events are not triggered. So for example in Page_Load:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataBindForm(); // which sets the `Text` property of this TextBox among others
}
}

I want RowCommand executed before RowCreate reloading OR something like this

I have
<asp:GridView>
<asp:TemplateField HeaderText="PsyHealth">
<ItemTemplate>
<asp:PlaceHolder runat="server" ID="PsyHealth" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="-">
<ItemTemplate>
<asp:LinkButton ID="Gen" CommandName="Gen" runat="server" Text="gen" />
</ItemTemplate>
</asp:TemplateField>
and
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var dataItem = e.Row.DataItem as ViewModels.UserTestorViewModel;
var psyHealth = e.Row.FindControl("PsyHealth") as PlaceHolder;
if (psyHealth != null)
{
psyHealth.Controls.Add(dataItem.PsyHeath);
}
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//bla bla bla
}
but when I clicked the Gen LinkButton on the page. The GridView1_RowCreated was triggered first and threw the error Object reference not set to an instance of an object because the e.Row.DataItem was null.
Edit: The Code Behind
protected void Page_Load(object sender, EventArgs e)
{
List<ViewModels.UserTestorViewModel> utViewModelList = new List<ViewModels.UserTestorViewModel> { };
utViewModelList = utRepo.GetUserTestorViewModelListByHrId();
this.GridView1.DataSource = utViewModelList;
this.GridView1.DataBind();
if (!IsPostBack)
{
}
}
protected void Page_Init(object sender, EventArgs e)
{
GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);
}
Can you store the utViewModelList in Session first time you get it? If so then you can get the UserTestorViewModel instance from saved by the selected row's DataKey value.
When you click any button in the gridview , your page is postbacked and the page load event is called before it goes into the RowCommand event. In the page load event you are binding your gridview again and that's why your RowCreated Event is called.
You have to bind your gridview under if (!IsPostBack)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<ViewModels.UserTestorViewModel> utViewModelList = new List<ViewModels.UserTestorViewModel> { };
utViewModelList = utRepo.GetUserTestorViewModelListByHrId();
this.GridView1.DataSource = utViewModelList;
this.GridView1.DataBind();
}
}
Edit: Now I got your issue after you posted code..
The problem is here in Page_Init, can you remove the event handler from here and try the following:
protected void Page_Init(object sender, EventArgs e)
{
GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);
}
add here
<asp:GridView onrowcreated="GridView1_RowCreated">

How do i know id of ListView item on OnItemUpdated?

I have suppliers table with id int value for each supplier. I'm trying to edit columns of this table inside of ListView.
i'm trying to access e arg but it doesn't have any data on id of the row i'm trying to update?
You need to set the ListView.DataKeyNames to contain the property name of the uniqueId
<asp:ListView runat="server" ID="LvSample" DataKeyNames="SupplierId"/>
Then the dataKey will be available in the update events. It is worth nothing that you might want to use the ItemUpdating event instead of the ItemUpdated as this happens before Update occurs.
protected void Page_Load(object sender, EventArgs e)
{
LvSample.ItemUpdating += new EventHandler<ListViewUpdateEventArgs>(LvSample_ItemUpdating);
LvSample.ItemUpdated += new EventHandler<ListViewUpdatedEventArgs>(LvSample_ItemUpdated);
}
void LvSample_ItemUpdated(object sender, ListViewUpdatedEventArgs e)
{
var supplierId = e.NewValues["SupplierId"];
}
void LvSample_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
var supplierId = e.Keys["SupplierId"];
}
use the sender instead of the e
protected void ListView1_ItemUpdated(object sender, ListViewUpdatedEventArgs e)
{
if ((sender as ListView) != null)
{
ListView l = (ListView)sender;
l.SelectedIndex;
l.etc..............
DataKey key = l.SelectedDataKey;
object k = key.Values["foo"];
}
}

Resources