-1

这可能是 Session 而不是 ToString() 的问题,我不确定。

我有两个 .aspx 页面,我想将数据表中的 IP 地址从一个页面传递到另一个页面。当我这样做时,会添加我不想要的空格。代码的简单版本是这样的:

第一个 .aspx 页面

int num = DropDownList1.SelectedIndex;
DataView tempDV = SqlDataSource2.Select(DataSourceSelectArguments.Empty) as DataView;

Session["camera"] = tempDV.Table.Rows[num].ItemArray[2];
Response.Redirect("test.aspx");

test.aspx 页面

string ipCamAdd = Session["camera"].ToString();

TextBox1.Text = "http://" + ipCamAdd + "/jpg/image.jpg?resolution=320x240";

我要打印的是

http ://ipadd/jpg/image.jpg?resolution=320x240

但打印出来的是

http//ipaddress      /jpg/image.jpg?resolution=320x240

我怎样才能解决这个问题?

另外,我问了这个问题,希望有人能告诉我为什么会这样。对不起这个错误。

4

3 回答 3

3

尝试这个:

string ipCamAdd = Session["camera"].Trim().ToString();

对于有效的关注, Session["camera"] 可能为空,将如下函数添加到您的代码中

    static string ToSafeString(string theVal)
    {
        string theAns;
        theAns = (theVal==null ? "" : theVal);
        return theAns;
    }

然后使用:

string ipCamAdd = Session["camera"].ToSafeString().Trim();

于 2014-08-27T17:01:07.900 回答
2

string.Replace如果您只想摆脱空格,则可以使用:

TextBox1.Text = "http://" + (ipCamAdd ?? "").Replace(" ", "") + "/jpg/image.jpg?resolution=320x240";
于 2014-08-27T17:01:53.030 回答
1

在设置为会话之前修剪结果。

Session["camera"] = tempDV.Table.Rows[num].ItemArray[2].Trim();

似乎在 SQL 中,您的数据类型是char(*)如果您将数据类型转换为varchar并重新输入数据,您将不会得到任何额外的空格

于 2014-08-27T17:02:15.930 回答