I have one Repeater with multiple rows.each row has one LinkButton and one HiddenField.
HiddenField value is bind at time of Repeater's Event OnItemDataBound.
My Question is that How can I pass this HiddenField Field Value with CommandArgument of this LinkButton?
Following is my source code.
<asp:Repeater ID="rptServiceRequestList" runat="server" OnItemCommand="rptServiceRequestList_ItemCommand" OnItemDataBound="rptServiceRequestList_ItemDataBound">
<ItemTemplate>
<asp:LinkButton ID="btnCustomerDeposit" runat="server" Text="Pay Deposit" CommandName="DepositFees" CommandArgument='<%# Eval("ServiceRequestId") %>'>
</asp:LinkButton>
<asp:HiddenField ID="hidAmount" runat="server" />
</asp:Repeater>
Please Help me. thank you to all in advance.
Yes you can set multiple command argument or the another way is you can use FindControl("hidAmount") method of repeater .
You can use below code.
HiddenField hdnAmount = (HiddenField)rptServiceRequestList.FindControl("hidAmount");
int amnt = Convert.ToInt32(hdnAmount.Value);
You can set multiple command argument(to send hidamount along with command Argument) as:
<asp:LinkButton ID="btnCustomerDeposit" runat="server" Text="Pay Deposit" CommandName="DepositFees" CommandArgument='<%#Eval("ServiceRequestId") + "|" +Eval("HidAmount")%>'
</asp:LinkButton>
And on ItemCommand:
protected void rptServiceRequestList_ItemDataBound(Object Sender, RepeaterCommandEventArgs e)
{
string[] arg = new string[2];
arg = e.CommandArgument.ToString().Split('|'); // Split Here to seprate CommandName And Hidden Value
string YourcommandName = arg[0]; // Your Command Name
string YourHiddenValue = arg[1]; // Your Hidden Field Value
}
Related
can we pass multiple eval field in one command argument.
my code is here
<asp:TemplateField HeaderText="Details" SortExpression="source">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%#Eval("source") %>' CommandName="Download" Text='<%#Eval("source") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
I want to be pass many Eval field in single command argument with command name
if possible please show any reference.
If this is what you are asking as you didnt provide any code snippet i'm assuming like this
CommandArgument='<%#Eval("ScrapId")+","+ Eval("UserId")%>'
In code behind you can use retrieve values like this
protected void GridViews_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Comment")
{
string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' });
string scrapid = commandArgs[0];
string uid = commandArgs[1];
}
}
You can do something like
<%# Eval("Param1").ToString() + Eval("Param2").ToString() %>
I have a Repeater that is binded with some DataTable. (Here I skiped header template).
<asp:Repeater ID="rpt_users" runat="server" OnItemCommand="rpt_users_ItemCommand" OnItemDataBound="rpt_users_ItemDataBound">
<ItemTemplate>
<tr class="c0">
<td><asp:CheckBox ID="CheckSelect" runat="server" /></td>
<td>
<asp:HyperLink ID="hpl_edit" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "name") %>'
NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "edit") %>'></asp:HyperLink></strong>
<asp:LinkButton ID="btn_del" runat="server" CommandName="Remove" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "key") %>'><img src="assets/img/delete.png" alt="<%#nodeDelete %>" title="<%#nodeDelete %>" class="ico-delpage icon-right" /></asp:LinkButton>
</td>
<td>
<p><%# DataBinder.Eval(Container.DataItem, "country") %></p>
</td>
<td>
<asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "daysleft") %>' OnTextChanged="Unnamed_TextChanged" AutoPostBack="true"/>
</td>
</tr>
</ItemTemplate>
<asp:Repeater>
I associated OnTextChanged event with a handler:
protected void Unnamed_TextChanged(object sender, EventArgs e)
{
var txt = (sender as TextBox).Text;
int newDays = 0;
try
{
newDays = int.Parse(txt);
}
catch { return; }
}
So, how to get the whole object that is associated with current row in Repeater? I need to get access to this object in my OnTextChanged event handler, because I need to get some data from this object that represents current row.
You cannot access the bound object of the repeater item in the OnTextChanged event. One way of doing this is as follows.
After retrieving the datatable, save the datatable in viewstate
ViewState["Data"] = data;
Bind the key value of the individual item to a hidden field by adding a hidden field to the repeater item
< asp:HiddenField runat="server" ID="hiddenFieldKey" Value='<%# DataBinder.Eval (Container.DataItem, "key") %>' />
You can get the, repeater item and then the hidden field, to get the key of the row. Then the other values can be retrieved by finding the rows in the table which is stored in the viewstate.
protected void Unnamed_TextChanged(object sender, EventArgs e)
{
var txt = (sender as TextBox).Text;
var repeaterItem = (sender as TextBox).NamingContainer as RepeaterItem;
var hiddenFieldKey =repeaterItem.FindControl("hiddenFieldKey") as HiddenField;
// Get data from viewstate
DataTable data = ViewState["Data"] as DataTable;
var dataRow= data.Rows.Find(hiddenFieldKey.Value);
//You can use this row to get the values of the other columns
int newDays = 0;
try
{
newDays = int.Parse(txt);
}
catch { return; }
}
Another way of doing is that, if you dont have many values to get, then bind all the required values in multiple hidden fields in the repeater item and then get those hidden field values in the OnTextChanged event
You can use the sender parameter, like you do for Text, var tb = (TextBox)sender;, then access that control's Parent property. You might have to go a couple levels up to find exactly what you're looking for, but that's the gist. Then you can use FindControl or whatever you need past that.
I have a Gridview dtAppend. I want that when I press delete button the selected row record should be deleted from users table.
I first used button field in gridview, as:
<asp:ButtonField Text="Delete" CommandName="DeleteRow" ControlStyle-CssClass="btn btn-danger btn-small" ControlStyle-ForeColor="White" />
<asp:TemplateField visible="false" ItemStyle-Width="0px">
<ItemTemplate>
<asp:HiddenField ID="HiddenField" Visible="false" runat="server" Value='<%# Eval("userId") %>' />
</ItemTemplate>
</asp:TemplateField>
My client says to show JavaScript alert and on clicking yes the record should be deleted. I cannot write onClientClick for button field so I am being forced to use normal Asp button.
on rowCommand of gridview I am getting the hidden field value in this code
if (e.CommandName == "DeleteRow")
{
GridViewRow row = dtAppend.Rows[Convert.ToInt32(e.CommandArgument)];
hidden1 = (HiddenField)row.Cells[6].FindControl("HiddenField");
string text = Convert.ToString((HiddenField)row.Cells[6].FindControl("HiddenField"));
Session["dtIdDel"] = hidden1.Value;
}
i am getting thew value in Session but i need above code working Button_ClickEvent like below
protected void deleteButton_Click(object sender, EventArgs e)
{
GridViewRow row = dtAppend.Rows[Convert.ToInt32(e.CommandArgument)];
hidden1 = (HiddenField)row.Cells[6].FindControl("HiddenField");
string text = Convert.ToString((HiddenField)row.Cells[6].FindControl("HiddenField"));
Session["dtIdDel"] = hidden1.Value;}
this is where 'e.CommandArgument' gives Error
I cannot use the above code in normal button click as it gives error in e.CommandArgument
Any help?
Simply you can remove visible="false"
<asp:HiddenField ID="HiddenField" runat="server" Value='<%# Eval("userId") %>' />
You Better Remove Visible="false" . Because, the value that has to be binded for hidden field will not be binded in to the field if Visible="false" is there. Any how its a hidden field, so make it Visible="true"
EDIT :
How you handled the RowDataBound event of the Grid, are you assigning the CommandArgument for each row, other wise the above concept will not work in Paging. Refer as below
Ex : -
Button btnMail = (Button)e.Row.FindControl("lnkMail");
btnMail.CommandArgument = e.Row.RowIndex.ToString();
I think this would be easy way, instead of using hidden field.
<asp:LinkButton CommandArgument='<%# Eval("userId") %>' OnClientClick="if (!confirm('Are you sure you want delete?')) return false;" CommandName="DeleteRow" ID="eliminar" runat="server" Text="delete"/>
if (e.CommandName == "DeleteRow")
{
int userId = Int32.Parse(e.CommandArgument.ToString());
}
You can simply send ID as command argument
or
Try the code as below:
var ID = int.Parse(((HiddenField)item.FindControl("HiddenField1")).Value);
sql = "delete from tablename where id=" + ID;
I was working on asp.net GridView control. Now I need to edit some row data. For that I was using this code:
<asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="QuickEdit" OnClick="btnEdit_Click"
CommandArgument ='<%# ((CheckBox)(((GridViewRow) Container).Cells[4].Controls[0])).Checked %>'/>
And the btnEdit_Click method is:
protected void btnEdit_Click(object sender,EventArgs e)
{
LinkButton btn = (LinkButton)sender;
switch (btn.CommandName)
{
case "QuickEdit":
EditPanel.Visible = true;
GridPanel.Visible = false;
CheckBox cbRequiresState = (CheckBox)EditPanel.FindControl("checkRequiresState");
if (btn.CommandArgument =="True")
{
cbRequiresState.Checked = true;
}
else
{
cbRequiresState.Checked = false;
}
break;
}
}
Now, I need to pass more than one argument as CommandArgument to that btnEdit_Click method. For that what I need to do?
And please suggest me a good way to utilize those arguments in that method.
here is an example :
in your aspx code :
<asp:ImageButton ID="btnSelect" runat="server" ImageUrl="~/Images/btnSelect.png" CommandName="Select" CommandArgument='<%# Container.DataItemIndex +";"+Eval("ID") %>' ToolTip="select" CausesValidation="False" /></ItemTemplate>
and in your code behind :
string info = e.CommandArgument.ToString();
string[] arg = new string[2];
char[] splitter = { ';' };
arg = info.Split(splitter);
You can use a string and separate the values by ; or another character.
Because CommandArgument is a simple string, concatenate the arguments you want to pass to the event putting some kind of separator among them.
Then in btnEdit_Click split the values by separator.
NOTE: Chose the sepatator so that it isn't a character contained in anyone of the parameters passed to the event.
I want to fetch the DataKey of the Selected Row of GridView on button click.
I would Personal do it in a Template Field Like so:
<asp:TemplateField>
<ItemTemplate>
//EDIT: after a comment it is suggested that you pass the RowIndex as the command argument which would provide access to the entire row
<asp:LinkButton ID="btnCopy" runat="server"CausesValidation="False"CommandName="MyCommandButton"CommandArgument='<%# Eval("MyDataKeyOrWhateverIWanteverIWantFromTheBindingSource")%>'>
</ItemTemplate>
</asp:TemplateField>
CodeBehind
protect void MyCommandButton(Object sender,CommandArgument e)
{
int DataKeyOrPK=int32.Parse(e.CommandArgument.ToString());
}
You other option would be:
<asp:gridview id="myGrid" runat="server"
width=100% datakeynames="Myid"
autogeneratecolumns=false
onSelectedIndexChanged="MyEvent">
<asp:templatefield headertext="Choose your dream home">
<itemtemplate>
<asp:linkbutton runat="server" commandname="select" text='<%# Eval ( "Whatever" ) %>' />
</itemtemplate>
</asp:templatefield>
Note the commandname="select" above.
Data-bound controls recognize certain command names and automatically raise and handle the appropriate events for the control. The following command names are recognized:
Cancel, Delete, Edit, Insert, New, Page, Select, Sort and Update. Reference
Codebehind
private void MyEvent(Object sender, EventArgs e)
{
string id = myGrid.SelectedDataKey.Value.ToString();
}
Use the SelectedDataKey property of the GridView:
DataKey currentKey = myGridView.SelectedDataKey;