Here is my grid view
<asp:GridView ID="grvClaimBanks" runat="server" AutoGenerateColumns="false" AllowPaging="false" OnRowCommand="grvClaimBanks_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Sr.No">
<ItemTemplate>
<%#Container.DataItemIndex +1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:TextBox ID="txtMoratoriumPeroid" runat="server">
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="lnkEditProduction" runat="server" CommandArgument='<%# Eval("TextileSLACId") %>' CommandName="EnterClaim" Text="Enter Claim" CausesValidation="false"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
Now i want to set value of "txtMoratoriumPeroid" as "CommandArgument" of "lnkEditProduction", so i can access that value in Row Command Event of grid view.
Is this possible?
If yes please help...
The way you are bind the TextBox wont give you value change in client side and your TextBox seem empty on server side. You can get current row of command button in RowCommand event and find TextBox to take its value.
GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
TextBox txtMoratoriumPeroid= row.FindControl("txtMoratoriumPeroid") as TextBox;
if(txtMoratoriumPeroid != null)
{
//Your code here.
string txtMoratoriumPeroidText = txtMoratoriumPeroid.Text;
}
No it is not possible to give dynamic CommandArgument on the fly.
But you can achieve this by
LinkButton lbtnSender = (LinkButton)sender;
TextBox txtMoratoriumPeroid = (TextBox)lbtnSender.Parent.Parent.FindControl("txtMoratoriumPeroid");
string MoratoriumPeroid = txtMoratoriumPeroid.Text;
Related
I have a GridView on which I am binding 2 textbox and a update button. grid view is dynamic so Row numbers can vary depending upon the size of dataset. Now what I want is when I change values in this textbox and click on update then data for that row in database should get updated. The textbox values in database should get updated.
This is my code:
<asp:GridView ID="dgAbstractSummary" runat="server" AutoGenerateColumns="False" OnRowDataBound="dgAbstractSummary_RowDataBound" OnRowCreated="dgAbstractSummary_RowCreated"
Visible="true">
<Columns>
<asp:BoundField HeaderText="ID" DataField="id" HtmlEncode="false"></asp:BoundField>
<asp:BoundField HeaderText="Abstract Title" DataField="title" HtmlEncode="false"></asp:BoundField>
<asp:TemplateField HeaderText="Significance Score">
<ItemTemplate>
<asp:TextBox ID="txtSignificanceScore" AutoPostBack="true" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"significanceScore")%>'></asp:TextBox>
<div class="voffset3"></div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Innovation Score">
<ItemTemplate>
<asp:TextBox ID="txtInnovationScore" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"innovationScore")%>'></asp:TextBox>
<div class="voffset3"></div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button AutoPostBack="true" CssClass="btn btn-primary btn-embossed" ID="btnUpdate" runat="server"
Text="Update"
OnClick="btnUpdate_Click" CommandArgument='<%#Eval("Id") %>' />
<div class="voffset3"></div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="panel-default"></HeaderStyle>
<RowStyle CssClass=""></RowStyle>
</asp:GridView>
C#
dgAbstractSummary.DataSource = dsResult; // dataset contains values which I am binding to grid columns
dgAbstractSummary.DataBind();
protected void btnUpdate_Click(object sender, EventArgs e)
{
Button myUpdateButton = (Button)sender;
}
Here Id (first column) is the unique key for each row.
Use find control function to find the two text boxes on the grid view row using your buttons sender event.
Something like this.
Button btn = (Button)sender;
GridViewRow gvr = (GridViewRow)btn.NamingContainer;
TextBox details = gvr.FindControl("detailsText") as TextBox;
//do something with details
TextBox cusID = gvr.FindControl("TextBox2") as TextBox;
How can I change the value of the label as a result of a multiplication from textbox and boundfield in gridview, whenever I change the value of the textbox? (not using Textbox_TextChanged, as it requires me to click elsewhere first before firing)
<asp:GridView ID="productView" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="productView_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="Sacks">
<ItemTemplate>
<asp:TextBox ID="txtSacks" runat="server"
CssClass="form-control" Text ="0" Width="100px"
OnTextChanged="txtSacks_TextChanged" AutoPostBack="true">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="pricingID.price" HeaderText="Price"/>
<asp:TemplateField HeaderText="Subtotal">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
lblTotal = txtSacks * price
Thanks in advance for any kind of help
Your current event handler will do a postback when the textbox loose focus. You may need some client side javascript to accomplish it without posting back on every key press. Drop in jQuery into the page and try this code:
<Columns>
<asp:TemplateField HeaderText="Sacks">
<ItemTemplate>
<asp:TextBox ID="txtSacks" runat="server"
CssClass="form-control" Text="0" Width="100px" ToolTip="<%# Container.DataItemIndex + 1 %>">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label runat="server" ID="lblPrice" CssClass="lbl-price" ToolTip="<%# Container.DataItemIndex + 1 %>" Text='<%# Bind("Price") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="price" HeaderText="Price" />
<asp:TemplateField HeaderText="Subtotal">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server" CssClass="lbl-total" ToolTip='<%# Container.DataItemIndex + 1 %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
Note that we are putting a data attribute on the controls with this code: data-sxid="sax-<%# Container.DataItemIndex + 1 %>" and data-lblid="sax-<%# Container.DataItemIndex + 1 %>"
Now we need some Javascript magic. Add this to the page:
<script>
$("input[title]").on("keyup", function () {
/*We had emitted ToolTip attribute on our controls in Grid.
Those are translated to `title` in the browser. Here we are
picking controls based on the values that would be set by
asp.net while rendering grid.*/
var $t = $(this),
//pick value of title or tooptip attribute of $t control
i = $t.attr("title") || $t.attr("tooltip");
//we get value of $t and initialize a number from it as it is a string
var s = Number($t.val());
//if we are not able get a number from textbox value, return
if (isNaN(s)) return;
//we are looking for a span which has title attribute set to value
//we picked from textbox above also having class lbl-price
//and getting its text
var p = $("span[title='" + i + "'].lbl-price").text();
//then we get next span with same title value but class lbl-total
// and set its text to calculated value.
$("span[title='" + i + "'].lbl-total").text(s * +p);
});
</script>
This is not the most elegant way to accomplish this but it does work. Long time since done anything with web-forms and gridviews!
Note: in place of <%# Container.DataItemIndex + 1 %>, you can use some property from data-row to string the controls like priceId.
I tried some ways in stackoverflow solutions. But Those are not implemented well by me. Most of the time I got null/empty for the variables and Exception like "Object reference not set to an instance of an object."
My GridView:
<asp:GridView runat="server" ID="TestReportGridView" AutoGenerateColumns="false" Width="370px">
<Columns>
<asp:TemplateField HeaderText="ID" Visible="false">
<ItemTemplate>
<%#Eval("TestId") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sr">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Test">
<ItemTemplate>
<%#Eval("TestName") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fee">
<ItemTemplate>
<%#Eval("Fee") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle HorizontalAlign="Left" />
</asp:GridView>
I want to put cell text in 4 different variables for each row when I click in Save button:
protected void SaveButton_Click(object sender, EventArgs e)
{
string patient = PatientNameTextBox.Text;
string birthDate = BirthDateTextBox.Text;
string mobile = MobileNoTextBox.Text;
int rows = TestReportGridView.Rows.Count;
foreach (GridViewRow row in TestReportGridView.Rows)
{
Label test = (Label)row.FindControl("Test");
string testName = test.Text;
//Label lblQuantity = (Label)row.FindControl("Quantity");
//string Quantity = lblQuantity.Text;
}
}
Image of the UI
I added a label in ItemTemplate. does it right way to add Label
<asp:TemplateField HeaderText="Test">
<ItemTemplate>
<asp:Label ID="Test" runat="server" ><%#Eval("TestName") %></asp:Label>
</ItemTemplate>
</asp:TemplateField>
You need to add Label control in your ItemTemplate. An example below:
<ItemTemplate>
<asp:Label ID="Test" runat="server" Text='<%# Eval("TestName") %>' />
</ItemTemplate>
Adding <%#Eval("TestId") %> directly inside ItemTemplate will not automatically add any control to search from code behind.
I have GridView and a button as follows. Then i am binding the gridview with data from my database. GridView has two hiddenfield for Id and ClassIndex.
when i selecting a checkbox and click button, i want to get the corresponding Id and FileName.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="check" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hdfId" runat ="server" Value='<%#Eval("Id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hdfClssIndex" runat ="server" Value='<%#Eval("ClassIndex") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblFileName" runat ="server" Text='<%#Eval("FileName") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and Button Like
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Send Request" />
the code behind button is
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
var check = row.FindControl("check") as CheckBox;
if (check.Checked)
{
int Id = Convert.ToInt32(row.Cells[1].Text);
//some logic follws here
}
}
}
but i am getting an error like
Input string was not in a correct format.
What is the error and how to solve it?
Your looping correct.
But you forgot to notice one thing here, when you wanted to access CheckBox you did a FindControl on row. Which means you are trying to find some control in that row.
Then why are you accessing HiddenField control inside row with row.Cell[1].Text?
Try to find that also.
int Id = Convert.ToInt32(((HiddenField)row.FindControl("hdfId")).Value);
I have added a button column to a gridview. I am populating the gridview with a datable through code then binding the datable to the grid.
I need to now look at the first column of the data and check if the text of the column is "NA" if it is the button in that column has to be disabled.....
How can I accomplish this? I am populating the data from code and the button is preadded to the grid in the markup
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:ButtonField Text="Delete" />
</Columns>
</asp:GridView>
GridView1.DataSource = dt;
GridView1.DataBind();
The best thing to do is implement the OnDataBinding method for a Button in a TemplateColumn.
Eg:
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" ID="btnDelete" CommandName="Delete"
Text="Delete" OnDataBinding="btnDelete_DataBinding" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Then in your codebehind implement your logic:
protected void btnDelete_DataBinding(object sender, System.EventArgs e)
{
Button btn = (Button)(sender);
btn.Enabled = !Eval("TheFieldInYourDataSourceToCompare").ToString().Equals("NA");
}
The advantage to doing it in this manner over the other posted answers:
No code in your markup
Code is localized to the Button control and can be reused if other Buttons require the same functionality.
Comparing to the DataSource value and not what the visual output is (this could tie into business logic for both rendering and checking).
Hope that helps.
Try this in the DataBound event handler:
protected void GridView1_DataBound(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (GridView1.Rows[i].Cells[1].Text == "NA")
{
// Disable the button
}
}
}
This is just a general idea. You'll have to modify the code for your application.
Don't forget to add OnDataBound="GridView1_DataBound" to the markup for the GridView.
Maybe something like this.
Did the button a little differently
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" ID="Btn_Delete" CommandName="Delete" Text="delete"
Enabled='<%# GridView1.Rows[0].Cells[1].Text != "NA" %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You'll want to you Eval("someColumn") most likely instead of GridView1.Rows[0].Cells[1].Text
Perhaps you can use OnRowDataBound="GridViewRowEventHandler" to set the value to enabled or disabled?
you can try from markup as,
<asp:TemplateField HeaderText="QA signature">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Column6") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("Column6") %>' Visible='<%# Eval("Column6") != "" %>' ></asp:Label>
<asp:Button ID="Button2" runat="server" Text="Sign Off" CssClass="cmdButton" Visible='<%# Eval("Column6") == "" %>' />
</ItemTemplate>
</asp:TemplateField>