I have the the GridView's column looking like that:
<Columns>
<asp:TemplateField HeaderText="Opcje">
<ItemTemplate>
<asp:LinkButton runat="server" Text="Accept" ID="AcceptBtn" CommandName="Accept"/>
<asp:LinkButton runat="server" Text="Deny" ID="DenyBtn" CommandName="Deny"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
while a new row is being created, I want to change both LinkButton's CommandArgument property:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
((LinkButton)e.Row.FindControl("AcceptBtn")).CommandArgument = myFiles[fileIndex].Name;
((LinkButton)e.Row.FindControl("DenyBtn")).CommandArgument = myFiles[fileIndex].Name;
}
The problem is, the code seems to be not changing anything, when I click on the AcceptBtn, the code below is invoked:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Accept")
{
string ss = (string)e.CommandArgument;
...
}
}
ss = "". Why ? If the page is PostedBack, both CommandArguments are cleared ?
Try setting the CommandArgument in the RowDataBound event instead of RowCreated.
you need to use rowdatabound event like..
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
((LinkButton)e.Row.FindControl("AcceptBtn")).CommandArgument = myFiles[fileIndex].Name;
((LinkButton)e.Row.FindControl("DenyBtn")).CommandArgument = myFiles[fileIndex].Name;
}
}
where do you set
myFiles[fileIndex].Name;
and each of its components?
Related
I have the grid view with Boundfield columns and am trying to replace Boundfield header text in code behind when the page loads. As per business, HeaderText has to change but in my case, the text is not changing immediately.
<asp:GridView ID="sampleGrid" runat="server" OnRowDataBound="sampleGrid_RowDataBound" OnSorting="sampleGrid_Sorting">
<Columns>
<asp:BoundField HtmlEncode="False" HeaderText="Name" DataField="Name" SortExpression="Name" />
</Columns>
</asp:GridView>
Code Behind:
protected void sampleGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row != null)
{
sampleGrid.Columns[0].HeaderText = "text";
}
}
I added main logic of my code. Please let me know if there is any solution for my issue.
Thanks in advance for the help!!
You can set the Value of the Header row on a Cell level in the RowDataBound event.
protected void sampleGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = "text";
}
}
Update
if (e.Row.RowType == DataControlRowType.Header)
{
LinkButton lb = e.Row.Cells[0].Controls[0] as LinkButton;
lb.Text = "text";
}
You may try the below
protected void sampleGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
LinkButton headerText = e.Row.Cells[0].Controls[0] as LinkButton;
headerText.Text = "Michel";
}
}
I am trying to find this control in the Gridview's edititemtemplate section.
<EditItemTemplate>
<ajaxToolkit:ComboBox ID="GridviewCategoryComboBox1" AppendDataBoundItems="true" runat="server" AutoCompleteMode="Suggest" DataSourceID="GridViewCategorySqlDataSource1" DataTextField="Name" DataValueField="Id" MaxLength="0" Style="display: inline;">
<asp:ListItem>Select Category</asp:ListItem>
</ajaxToolkit:ComboBox>
Here is the event handler where I try to fetch the control that is in the edititem template.
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs
e)
{
GridView1.EditIndex = e.NewEditIndex;
int id = (int)GridView1.DataKeys[e.NewEditIndex].Value;
ComboBox ddl = GridView1.Rows[e.NewEditIndex].Cells[1].FindControl("GridviewCategoryComboBox1") as ComboBox;
}
It returns null, no matter who I try to find it.
I also tried other variations such as this:
ComboBox ddl = GridView1.Rows[e.NewEditIndex].FindControl("GridviewCategoryComboBox1") as ComboBox;
You can use the RowDataBound event for this:
protected void GridView1_RowDataBound(object sender, GridViewEditEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
if ((e.Row.RowState & DataControlRowState.Edit) > 0) {
ComboBox ddl = (ComboBox)e.Row.FindControl("GridviewCategoryComboBox1");
}
}
}
Because it is likely that you have other code in the RowDataBound event, then this allows you to centralize all your code in that event and avoid duplicate code.
<asp:TemplateField HeaderText="Type">
<ItemTemplate>
<asp:TextBox ID="TypeID" runat="server" Text='<%# Eval("Type") %>' />
</ItemTemplate>
</asp:TemplateField>
I want to remove this attribute Text='<%# Eval("Type") %>' programmatically on page load.
You can do it in the RowDatabound as shown below :
protected void GridViewName_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Hide the Textbox
TextBox txtboxtobehidden=
(TextBox)e.Row.FindControl("txtboxtobehidden");
txtboxtobehidden.Visible = false;
}
}
catch (Exception ex)
{
// your error Code
}
}
To avoid null exception on binding you can choose one option below:
You can change Text attribute in HTML Markup like:
Text='<%# Eval("Type").Tostring() == string.Empty ? "NA" : Eval("Type") %>'
Or, you can do it in Code-Behind like in RowDataBound event:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// find your textbox control in gridview
TextBox tb = (TextBox)e.Row.FindControl("txtboxtobehidden");
if(tb.Text == "")
{
// To remove Text attribute
tb.Attributes.Remove("Text");
// tb.Text = "NA";
}
}
}
I have a problem with my code. I need enable or disable a HyperLink in the RowDataBound event in an ASP.NET GridView based on a value extracted from the database.
If the value of field File of my database is not null, the HyperLink is visible otherwise not. In GridView, I don't have planned to show the value of field File.
I've tried using these solution without success, because I have this error.
Compiler Error Message: CS1502: The best overloaded method match for 'string.IsNullOrEmpty(string)' has some invalid arguments
Here is my code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink Srl = (HyperLink)e.Row.FindControl("Srl");
foreach (string color in colorList)
{
if (!string.IsNullOrEmpty(DataBinder.Eval(e.Row.DataItem, "File")))
{
Srl.Visible = true;
}
}
}
}
DataBinder.Eval returns an object. You need to convert that to a String.
Try this:
if (!string.IsNullOrEmpty(Convert.ToString(DataBinder.Eval(e.Row.DataItem, "File"))))
{
Srl.Visible = true;
}
<asp:GridView ID="gvList" runat="server" Width="100%" AutoGenerateColumns="False" AllowPaging="false">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="cbSelect" runat="server" />
<asp:HiddenField ID="hfSLNO_BARCODE" runat="server"
Value='<%#Bind("SLNO_BARCODE") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Use asp:HiddenField for File value and use this in row data bound event.
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink Srl = (HyperLink)e.Row.FindControl("Srl");
foreach (string color in colorList)
{
string str = (string)DataBinder.Eval(e.Row.DataItem, "File");
//then you can check
if (!String.IsNullorEmpty(str ))
{
Srl.Visible = true;
}
}
}
}
I have a TextBox control inside a panel and this panel is inside DataList ItemTemplate.
After firing the ItemCommand event, everything works fine except that the TextBox.Text property is always an empty string "" although there is some text in it.
I tried several ways but without success. I would really appreciate if someone can assist me with this. Simplified code is shown below.
Thank you!
ASPX page:
<asp:DataList ID="dlDataList" runat="server" onitemcommand="dlDataList_ItemCommand">
<ItemTemplate>
<asp:Panel ID="pnlReply" runat="server" Visible="False">
<asp:TextBox ID="txtTextBox" runat="server"></asp:TextBox><br />
<asp:LinkButton ID="lnkbtnSend" CommandName="Send" runat="server">Send</asp:LinkButton>
</asp:Panel><br />
<asp:LinkButton ID="OpenPanel" CommandName="OpenPanel" runat="server">Open panel</asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</asp:Content>
ASPX.CS Page code behind
protected void Page_Load(object sender, EventArgs e)
{
FillDataList();
}
private void FillDataList()
{
List<string> list = new List<string>();
list.Add("First");
list.Add("Second");
list.Add("Third");
dlDataList.DataSource = list;
dlDataList.DataBind();
}
protected void dlDataList_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "OpenPanel")
{
Panel pnlReply = (Panel)e.Item.FindControl("pnlReply");
pnlReply.Visible = true;
}
if (e.CommandName == "Send")
{
TextBox txtTextBox = (TextBox)e.Item.FindControl("txtTextBox");
//I tried this way also..
//TextBox txtTextBox = (TextBox)e.item.FindControl("pnlReady").FindControl("txtTextBox");
Label1.Text = txtTextBox.Text;
}
}
Please use IsPostBack in page load event. Without it your FillDataList(); is executing on every postback and resetting your DataList.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillDataList();
}
}