Gridview rowupdating doesn't work - asp.net

i have a simple task of updating row, on a click of update linkbutton, as
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" DataKeyNames="ID">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<% #Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<% #Eval("Mobile") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<% #Eval("LName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<% #Eval("LName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="action">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit">LinkButton</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server">Update</asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
at CS page
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox t1 = GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox;
string s = t1.Text.ToString();
Label3.Text = s;
}
I tried working on this several times but i don't get any answer. what possible code i am missing or there is any error in a code.Thanks for any assistance.

Your LinkButton needs to have a CommandName applied to it in the EditTemplate:
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Update">Update</asp:LinkButton>

Related

Getting selectedvalue of radiobuttonlist in gridview on selectedindexchanged

There is a radiobuttonlist in a gridview. I want to do something with the selectedvalue of which radiobuttonlist's selectedindexchanged.
But it's giving the old value always. For example the first item is selected when page load. If I click the second item, its still giving the first item's value.
My codes, in aspx file:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="cezaDS" >
<Columns>
<asp:TemplateField HeaderText="Öğrenci Bilgileri" SortExpression="SINIF">
<ItemTemplate>
<asp:Label ID="lbSiraNo" Visible="false" runat="server" Text='<%# Eval("SIRA_NO") %>'></asp:Label>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("ADI_SOYADI") %>'></asp:Label><br />
<asp:Label ID="Label3" runat="server" Text='<%# Bind("OKUL_NO") %>'></asp:Label><br />
<asp:Label ID="Label2" runat="server" Text='<%# Bind("BOLUM") %>'></asp:Label><br />
<asp:Label ID="Label1" runat="server" Text='<%# Bind("SINIF") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Ceza Türü ve Aldığı Zaman" SortExpression="DONEM">
<ItemTemplate>
Ceza Türü: <b><asp:Label ID="Label7" runat="server" Text='<%# Bind("CEZA_TURU") %>'></asp:Label></b><br />
Cezayı Aldığı Sınıf: <b><asp:Label ID="Label6" runat="server" Text='<%# Bind("CEZA_ALDIGI_SINIF") %>'></asp:Label></b><br />
Cezayı Aldığı Öğretim Yılı: <b><asp:Label ID="Label5" runat="server" Text='<%# Bind("DONEM") %>'></asp:Label></b>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ILGILI_MADDE" HeaderText="İlgili Madde" SortExpression="ILGILI_MADDE" />
<asp:TemplateField HeaderText="Kararınız">
<ItemTemplate>
<asp:RadioButtonList ID="rdList" runat="server"
AutoPostBack="True" OnSelectedIndexChanged="rdList_SelectedIndexChanged">
<asp:ListItem Value="Kaldırılsın">Kaldırılsın</asp:ListItem>
<asp:ListItem Value="Kaldırılmasın">Kaldırılmasın</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In cs file:
protected void rdList_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = (GridViewRow)(((RadioButtonList)sender).NamingContainer);
Label cezaSiraNo = (Label)(row.FindControl("lbSiraNo"));
RadioButtonList clickedList= (RadioButtonList)sender;
string x = clickedList.SelectedValue;
//...
Here x variable is taking the wrong value.

Cells text always getting empty string("") in Gridview rowdatabound

I'm trying to do the disable the delete button for the admin role in the list of users which I had bound in gridview.
So, I went with rowdatabound option where I can disable based on the condition as like checking the text of the role column.
If the role columns text is "Admin" then I had to disable the "Delete" button which I had customized.
Unfortunately I'm not able to get the text of the cells while fetching the data.
I had used the statement as like below :
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btn = (e.Row.FindControl("Delbutton") as Button);
if(e.Row.Cells[7].Text == "ADMIN")
{
btn.Enabled = false;
}
}
}
My aspx page :
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
OnPageIndexChanging="GridView1_PageIndexChanging"
PagerSettings-Mode="Numeric"
PagerSettings-PreviousPageText="Previous"
PagerSettings-PageButtonCount="15"
PagerSettings-NextPageText="Next"
PagerStyle-ForeColor="WhiteSmoke"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:button ID="button1" runat="server" Text="Select" OnClick="button1_Click" ></asp:button>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="id" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label ID="idemail" runat="server" Text='<%#Eval("Email") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="idName" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Passwor">
<ItemTemplate>
<asp:Label ID="idPassword" runat="server" Text='<%#Eval("Passwor") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MobileNumber">
<ItemTemplate>
<asp:Label ID="idMobileNumber" runat="server" Text='<%#Eval("MobileNumber") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gender">
<ItemTemplate>
<asp:Label ID="idGender" runat="server" Text='<%#Eval("Gender") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Qulification">
<ItemTemplate>
<asp:Label ID="idQulification" runat="server" Text='<%#Eval("Qulification") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Role">
<ItemTemplate>
<asp:Label ID="idRole" runat="server" Text='<%#Eval("Role") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:button ID="Delbutton" runat="server" Text="Delete" OnClientClick="return confirm('Are you sure, Do you want to delete.')" OnClick="Delbutton_Click" ></asp:button>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings NextPageText="Next" PageButtonCount="15" PreviousPageText="Previous"></PagerSettings>
</asp:GridView>
Please check the image of my code on debugger
This was my data on page after loaded

ASP.NET how to hide template field control based on another template field value in GridView

here my code:
<asp:GridView ID="_gvPLCs" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
CellPadding="4" DataSourceID="_dsPLCs" ForeColor="Black" GridLines="Horizontal"
onrowcommand="_gvPLCs_RowCommand" onrowdatabound="_gvPLCs_RowDataBound">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="lbnView" runat="server" CausesValidation="false"
CommandName="view" Text="View manual"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PLCName" SortExpression="PLCName">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("PLCName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("PLCName") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PLCManual" SortExpression="PLCManual">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("PLCManual") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("PLCManual") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LabName" SortExpression="LabName">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("LabName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("LabName") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MachineName" SortExpression="MachineName">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("MachineName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("MachineName") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MaxAllowedToResDay"
SortExpression="MaxAllowedToResDay">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("MaxAllowedToResDay") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server"
Text='<%# Bind("MaxAllowedToResDay") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
</asp:GridView>
code behind:
protected void _gvPLCs_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
return;
if (e.Row.Cells[2].Text == " ")
{
LinkButton lbnview = (LinkButton)e.Row.FindControl("lbnView");
lbnview.Visible = false;
}
}
this doesn't work with me..plz help
what i need is : when there is no a manual;the view manual link disappear
i tested it using this code :
if (e.Row.Cells[2].Text == string.Empty)
{
LinkButton lbnview = (LinkButton)e.Row.FindControl("lbnView");
lbnview.Visible = false;
}
but it always give me "" and always manaul field disappears
try this:
<asp:LinkButton ID="lbnView" runat="server" CausesValidation="false" CommandName="view" Text="View manual" Visible='<%# !string.IsNullOrEmpty(Bind("PLCName").ToString()) %>'></asp:LinkButton>

'GridView1' fired event PageIndexChanging which wasn't handled

I am using a gridview and I want to use paging. I have already set allow paging to true and page size to 5. I can see the numbers at the base of my gridview, but when i click on a number to move to respective page, it throws an error saying:
The GridView 'GridView1' fired event PageIndexChanging which wasn't handled.
Code:
<asp:GridView ID="GridView1" runat="server" CellPadding="5"
AutoGenerateColumns="False" AllowPaging="True" DataKeyNames="contact_id"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
PageSize="5">
<Columns>
<asp:TemplateField HeaderText="contact_id">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("contact_id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="name">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="address">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("address") %>'></asp:Label><br />
<asp:Label ID="Label6" runat="server" Text='<%# Eval("city") %>'></asp:Label><br />
<asp:Label ID="Label7" runat="server" Text='<%# Eval("state") %>'></asp:Label><br />
<asp:Label ID="Label8" runat="server" Text='<%# Eval("pincode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="email">
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Eval("email") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="mobile">
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text='<%# Eval("mobile") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="context">
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Text='<%# Eval("context") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="status">
<ItemTemplate>
<asp:Label ID="Label12" runat="server" Text='<%# Eval("status") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>PENDING</asp:ListItem>
<asp:ListItem>OK</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemStyle CssClass="button" />
</asp:TemplateField>
</Columns>
<PagerStyle HorizontalAlign="Left" VerticalAlign="Middle" />
</asp:GridView>
You will have to handle the PageIndexChanging event for the grid
Something like
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
//Bind grid
}
It will not work if you enable paging. You also need to write the event handler for PageIndexChangeEvent. Check this link: http://forums.asp.net/post/1177923.aspx
You would need to code "PageIndexChanging" event to make it work. Add an event handler for the PageIndexChanging where you set the GridView.CurrentPage = e.NewPage...
Add one more event in HTML mark up for pagging.
OnPageIndexChanging="GridView1_PageIndexChanging"
Now handle same event from code behind
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//Your code
}

Display text in multiline inside a grid view in asp.net

I am using a grid view in my asp.net application. In one column i need to display description(minimum 5 characters. Maximum 255 characters).i am using a label to hold description in that grid view.
But my problem is that if the description is larger it stretches in the browser and show it in one line. I want to display description in multi line (like a paragraph)
I hope some one help me . the entire grid view code is shown below
<asp:GridView ID="gv_View_Documents" runat="server" AllowSorting="true" DataKeyNames="DocumentName,Description" SkinID="customGridview" AutoGenerateColumns="false" OnSorting="gv_View_Documents_Sorting" OnRowCancelingEdit="gv_View_Documents_RowCancelingEdit" OnRowCommand="gv_View_Documents_RowCommand"
OnRowEditing="gv_View_Documents_RowEditing" OnRowUpdating="gv_View_Documents_RowUpdating" >
<Columns>
<asp:TemplateField HeaderText="Document Name" HeaderStyle-Width="200" HeaderStyle-CssClass="GridHeaderStyle" SortExpression="DocumentName" >
<ItemTemplate>
<asp:LinkButton CommandName="ViewDocument" CssClass="GridHeaderStyle" ID="hlnk_View_Document" runat="server" CommandArgument='<%# Bind("DocumentName") %>' Text='<%# Bind("DocumentName") %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="200" HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lbl_gv_DocumentDescription" runat="server" Text='<%# Bind("Description") %>' ></asp:Label></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_gv_EditDescription" MaxLength="250" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="50" HeaderStyle-CssClass="GridHeaderStyle" ShowHeader="False" >
<EditItemTemplate>
<asp:LinkButton ID="Bttn_Update_Description" ForeColor=" #555555" runat="server" CausesValidation="False"
CommandName="Update" Text="Update"></asp:LinkButton> <asp:LinkButton ID="Bttn_Cancel_Settings" ForeColor=" #555555" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton></EditItemTemplate><ItemTemplate>
<asp:LinkButton ID="Bttn_Edit_Description" ForeColor=" #555555" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit" ></asp:LinkButton></ItemTemplate><ControlStyle CssClass="edit" />
</asp:TemplateField>
</Columns>
</asp:GridView>
try this... working properly...for wrapping text in Gridview
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].Attributes.Add("style", "word-break:break-all;word-wrap:break-word;width:100px");
}
}
}
Sometimes ItemStyle Wrap="true" doesn't work. To be certain your text wraps, surround the Label in a and set a width on the surrounding div.
EDIT
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text="<%# Eval("ID") %>"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<div style="width:100px;">
<asp:Label ID="Label2" runat="server" Text="<%# Eval("Name") %>"></asp:Label>
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text="<%# Eval("Age") %>"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
You can set the ItemStyle of the TemplateField to true like this:
<ItemStyle Wrap="true" Width="100px" />
Complete gridview code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle Wrap="true" Width="100px" />
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("Age") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Screenshot:
Try like this by applying the css class
.paraGraphtext
{
white-space: pre-wrap;
}
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text="<%# Eval("ID") %>" CssClass="paraGraphtext"></asp:Label>
</ItemTemplate>

Resources